File: operator_fallback_gpu_test.cc

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (82 lines) | stat: -rw-r--r-- 2,580 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>

#include "caffe2/core/operator.h"
#include "caffe2/operators/operator_fallback_gpu.h"
#include <gtest/gtest.h>

namespace caffe2 {


class IncrementByOneOp final : public Operator<CPUContext> {
 public:
  template <class... Args>
  explicit IncrementByOneOp(Args&&... args)
      : Operator<CPUContext>(std::forward<Args>(args)...) {}
  bool RunOnDevice() override {
    const auto& in = Input(0);

    auto* out = Output(0, in.sizes(), at::dtype<float>());
    const float* in_data = in.template data<float>();
    float* out_data = out->template mutable_data<float>();
    for (int i = 0; i < in.numel(); ++i) {
      out_data[i] = in_data[i] + 1.f;
    }
    return true;
  }
};


OPERATOR_SCHEMA(IncrementByOne)
    .NumInputs(1).NumOutputs(1).AllowInplace({{0, 0}});

REGISTER_CPU_OPERATOR(IncrementByOne, IncrementByOneOp);
REGISTER_CUDA_OPERATOR(IncrementByOne, GPUFallbackOp);

TEST(OperatorFallbackTest, IncrementByOneOp) {
  OperatorDef op_def = CreateOperatorDef(
      "IncrementByOne", "", vector<string>{"X"},
      vector<string>{"X"});
  Workspace ws;
  Tensor source_tensor(vector<int64_t>{2, 3}, CPU);
  for (int i = 0; i < 6; ++i) {
    source_tensor.mutable_data<float>()[i] = i;
  }
  BlobGetMutableTensor(ws.CreateBlob("X"), CPU)->CopyFrom(source_tensor);
  unique_ptr<OperatorBase> op(CreateOperator(op_def, &ws));
  EXPECT_TRUE(op.get() != nullptr);
  EXPECT_TRUE(op->Run());
  const TensorCPU& output = ws.GetBlob("X")->Get<TensorCPU>();
  EXPECT_EQ(output.dim(), 2);
  EXPECT_EQ(output.size(0), 2);
  EXPECT_EQ(output.size(1), 3);
  for (int i = 0; i < 6; ++i) {
    EXPECT_EQ(output.data<float>()[i], i + 1);
  }
}

TEST(OperatorFallbackTest, GPUIncrementByOneOp) {
  if (!HasCudaGPU()) return;
  OperatorDef op_def = CreateOperatorDef(
      "IncrementByOne", "", vector<string>{"X"},
      vector<string>{"X"});
  op_def.mutable_device_option()->set_device_type(PROTO_CUDA);
  Workspace ws;
  Tensor source_tensor(vector<int64_t>{2, 3}, CPU);
  for (int i = 0; i < 6; ++i) {
    source_tensor.mutable_data<float>()[i] = i;
  }
  BlobGetMutableTensor(ws.CreateBlob("X"), CUDA)->CopyFrom(source_tensor);
  unique_ptr<OperatorBase> op(CreateOperator(op_def, &ws));
  EXPECT_TRUE(op.get() != nullptr);
  EXPECT_TRUE(op->Run());
  const TensorCUDA& output = ws.GetBlob("X")->Get<TensorCUDA>();
  Tensor output_cpu(output, CPU);
  EXPECT_EQ(output.dim(), 2);
  EXPECT_EQ(output.size(0), 2);
  EXPECT_EQ(output.size(1), 3);
  for (int i = 0; i < 6; ++i) {
    EXPECT_EQ(output_cpu.data<float>()[i], i + 1);
  }
}

}  // namespace caffe2