@@ -46,8 +46,12 @@ limitations under the License.
4646#include " tensorflow/core/public/version.h"
4747#include " tensorflow/core/util/tensor_slice_reader_cache.h"
4848
49- namespace tensorflow {
49+ #ifdef GOOGLE_CUDA
50+ #include " cuda/include/cuda.h"
51+ #include " tensorflow/core/common_runtime/gpu/gpu_managed_allocator.h"
52+ #endif
5053
54+ namespace tensorflow {
5155namespace test {
5256
5357inline void SetOutputAttrs (OpKernelContext::Params* params,
@@ -71,14 +75,16 @@ inline void SetOutputAttrs(OpKernelContext::Params* params,
7175// to use the BrainClient interface.
7276class OpsTestBase : public ::testing::Test {
7377 public:
74- OpsTestBase () : device_type_( DEVICE_CPU ) {
75- device_. reset (
76- DeviceFactory::NewDevice ( " CPU " , {}, " /job:a/replica:0/task:0 " ));
78+ OpsTestBase ()
79+ : device_(DeviceFactory::NewDevice( " CPU " , {}, " /job:a/replica:0/task:0 " )),
80+ device_type_ ( DEVICE_CPU ) {
7781 CHECK (device_.get ()) << " Could not create CPU device" ;
82+ allocator_ = device_->GetAllocator (AllocatorAttributes ());
7883 }
7984
8085 ~OpsTestBase () override {
8186 gtl::STLDeleteElements (&tensors_);
87+ gtl::STLDeleteElements (&managed_outputs_);
8288 context_.reset (nullptr );
8389 params_.reset (nullptr );
8490 }
@@ -89,6 +95,18 @@ class OpsTestBase : public ::testing::Test {
8995 CHECK (device_.get ()) << " No device provided" ;
9096 device_type_ = device_type;
9197 device_ = std::move (device);
98+ #ifdef GOOGLE_CUDA
99+ if (device_type == DEVICE_GPU ) {
100+ managed_allocator_.reset (new GpuManagedAllocator ());
101+ allocator_ = managed_allocator_.get ();
102+ } else {
103+ managed_allocator_.reset ();
104+ allocator_ = device_->GetAllocator (AllocatorAttributes ());
105+ }
106+ #else
107+ CHECK_NE (device_type, DEVICE_GPU )
108+ << " Requesting GPU on binary compiled without GOOGLE_CUDA." ;
109+ #endif
92110 }
93111
94112 void set_node_def (const NodeDef& node_def) { node_def_.CopyFrom (node_def); }
@@ -118,64 +136,22 @@ class OpsTestBase : public ::testing::Test {
118136 // TODO(vrv): Replace with something like a BrainClient Feed.
119137 template <typename T>
120138 void AddInput (const TensorShape& shape, std::function<T(int )> input_mapping) {
121- CHECK_GT (input_types_.size (), inputs_.size ())
122- << " Adding more inputs than types; perhaps you need to call MakeOp" ;
123- bool is_ref = IsRefType (input_types_[inputs_.size ()]);
124- Tensor* input = new Tensor (device_->GetAllocator (AllocatorAttributes ()),
125- DataTypeToEnum<T>::v (), shape);
126- test::FillFn (input, input_mapping);
127- tensors_.push_back (input);
128- if (is_ref) {
129- CHECK_EQ (RemoveRefType (input_types_[inputs_.size ()]),
130- DataTypeToEnum<T>::v ());
131- inputs_.push_back ({&lock_for_refs_, input});
132- } else {
133- CHECK_EQ (input_types_[inputs_.size ()], DataTypeToEnum<T>::v ());
134- inputs_.push_back ({nullptr , input});
135- }
139+ test::FillFn (AddInput (DataTypeToEnum<T>::v (), shape), input_mapping);
136140 }
137141
138142 // Like AddInput but takes in an explicit arrayslice of data.
139143 template <typename T>
140144 void AddInputFromArray (const TensorShape& shape,
141145 const gtl::ArraySlice<T>& data) {
142- CHECK_GT (input_types_.size (), inputs_.size ())
143- << " Adding more inputs than types; perhaps you need to call MakeOp" ;
144- bool is_ref = IsRefType (input_types_[inputs_.size ()]);
145- Tensor* input = new Tensor (device_->GetAllocator (AllocatorAttributes ()),
146- DataTypeToEnum<T>::v (), shape);
147- test::FillValues<T>(input, data);
148- tensors_.push_back (input);
149- if (is_ref) {
150- CHECK_EQ (RemoveRefType (input_types_[inputs_.size ()]),
151- DataTypeToEnum<T>::v ());
152- inputs_.push_back ({&lock_for_refs_, input});
153- } else {
154- CHECK_EQ (input_types_[inputs_.size ()], DataTypeToEnum<T>::v ());
155- inputs_.push_back ({nullptr , input});
156- }
146+ test::FillValues<T>(AddInput (DataTypeToEnum<T>::v (), shape), data);
157147 }
158148
159149 // Convenience function to add an input and populate it with the elements from
160150 // an initializer list converting the types as needed.
161151 template <typename T, typename SrcType>
162152 void AddInputFromList (const TensorShape& shape,
163153 std::initializer_list<SrcType> data) {
164- CHECK_GT (input_types_.size (), inputs_.size ())
165- << " Adding more inputs than types; perhaps you need to call MakeOp" ;
166- bool is_ref = IsRefType (input_types_[inputs_.size ()]);
167- Tensor* input = new Tensor (device_->GetAllocator (AllocatorAttributes ()),
168- DataTypeToEnum<T>::v (), shape);
169- test::FillValues<T>(input, data);
170- tensors_.push_back (input);
171- if (is_ref) {
172- CHECK_EQ (RemoveRefType (input_types_[inputs_.size ()]),
173- DataTypeToEnum<T>::v ());
174- inputs_.push_back ({&lock_for_refs_, input});
175- } else {
176- CHECK_EQ (input_types_[inputs_.size ()], DataTypeToEnum<T>::v ());
177- inputs_.push_back ({nullptr , input});
178- }
154+ test::FillValues<T>(AddInput (DataTypeToEnum<T>::v (), shape), data);
179155 }
180156
181157 // Adds a Resource type as input. If <container> is empty, uses the default
@@ -197,8 +173,7 @@ class OpsTestBase : public ::testing::Test {
197173 handle.set_name (name);
198174 handle.set_hash_code (type_index.hash_code ());
199175 handle.set_maybe_type_name (type_index.name ());
200- Tensor* input = new Tensor (device_->GetAllocator (AllocatorAttributes ()),
201- DT_RESOURCE , TensorShape ({}));
176+ Tensor* input = new Tensor (allocator (), DT_RESOURCE , TensorShape ({}));
202177 input->scalar <ResourceHandle>()() = handle;
203178 tensors_.push_back (input);
204179 inputs_.push_back ({nullptr , input});
@@ -248,17 +223,54 @@ class OpsTestBase : public ::testing::Test {
248223 // REQUIRES: 0 <= output_index < context_->num_outputs()
249224 Tensor* GetOutput (int output_index) {
250225 CHECK_LT (output_index, context_->num_outputs ());
251- return context_->mutable_output (output_index);
226+ Tensor* output = context_->mutable_output (output_index);
227+ #ifdef GOOGLE_CUDA
228+ if (device_type_ == DEVICE_GPU ) {
229+ managed_outputs_.resize (context_->num_outputs ());
230+ // Copy the output tensor to managed memory if we haven't done so.
231+ if (!managed_outputs_[output_index]) {
232+ Tensor* managed_output =
233+ new Tensor (allocator (), output->dtype (), output->shape ());
234+ auto src = output->tensor_data ();
235+ auto dst = managed_output->tensor_data ();
236+ CHECK_EQ (
237+ cuMemcpyDtoD (reinterpret_cast <CUdeviceptr>(dst.data ()),
238+ reinterpret_cast <CUdeviceptr>(src.data ()), src.size ()),
239+ CUDA_SUCCESS );
240+ CHECK_EQ (cuCtxSynchronize (), CUDA_SUCCESS );
241+ managed_outputs_[output_index] = managed_output;
242+ }
243+ output = managed_outputs_[output_index];
244+ }
245+ #endif
246+ return output;
252247 }
253248
254- Allocator* allocator () {
255- return device_->GetAllocator (AllocatorAttributes ());
256- }
249+ Allocator* allocator () { return allocator_; }
257250
258251 const DataTypeVector& output_types () const { return kernel_->output_types (); }
259252
253+ private:
254+ Tensor* AddInput (DataType dtype, const TensorShape& shape) {
255+ CHECK_GT (input_types_.size (), inputs_.size ())
256+ << " Adding more inputs than types; perhaps you need to call MakeOp" ;
257+ bool is_ref = IsRefType (input_types_[inputs_.size ()]);
258+ Tensor* input = new Tensor (allocator (), dtype, shape);
259+ tensors_.push_back (input);
260+ if (is_ref) {
261+ CHECK_EQ (RemoveRefType (input_types_[inputs_.size ()]), dtype);
262+ inputs_.push_back ({&lock_for_refs_, input});
263+ } else {
264+ CHECK_EQ (input_types_[inputs_.size ()], dtype);
265+ inputs_.push_back ({nullptr , input});
266+ }
267+ return input;
268+ }
269+
260270 protected:
261271 std::unique_ptr<Device> device_;
272+ // The device allocator, or the managed_allocator_ below if running on GPU.
273+ Allocator* allocator_;
262274
263275 std::unique_ptr<OpKernel> kernel_;
264276 std::unique_ptr<ScopedStepContainer> step_container_;
@@ -271,9 +283,13 @@ class OpsTestBase : public ::testing::Test {
271283 gtl::InlinedVector<TensorValue, 4 > inputs_;
272284 // Owns Tensors.
273285 std::vector<Tensor*> tensors_;
286+ // Copies of the outputs in unified memory (host and device accessible).
287+ std::vector<Tensor*> managed_outputs_;
274288
275289 std::unique_ptr<OpKernelContext::Params> params_;
276290 std::unique_ptr<OpKernelContext> context_;
291+ // Unified memory allocator, only used when running on GPU.
292+ std::unique_ptr<Allocator> managed_allocator_;
277293
278294 private:
279295 TF_DISALLOW_COPY_AND_ASSIGN (OpsTestBase);
0 commit comments