File: in_batch_broadcast_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 (229 lines) | stat: -rw-r--r-- 7,696 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <gtest/gtest.h>
#include "caffe2/core/common.h"
#include "caffe2/core/logging.h"
#include "caffe2/opt/custom/in_batch_broadcast.h"
#include "caffe2/utils/proto_utils.h"

using namespace caffe2;
namespace {

void checkNet(NetDef& net, NetDef& expected_net) {
  TORCH_CHECK_EQ(net.op().size(), expected_net.op().size());
  for (int i = 0; i < net.op().size(); i++) {
    auto& op1 = net.op(i);
    auto& op2 = expected_net.op(i);
    TORCH_CHECK_EQ(op1.type(), op2.type());
    TORCH_CHECK_EQ(op1.input_size(), op2.input_size());
    TORCH_CHECK_EQ(op1.output_size(), op2.output_size());
    for (int j = 0; j < op1.input_size(); j++) {
      TORCH_CHECK_EQ(op1.input(j), op2.input(j));
    }
    for (int j = 0; j < op1.output_size(); j++) {
      TORCH_CHECK_EQ(op1.output(j), op2.output(j));
    }
    TORCH_CHECK_EQ(
        op1.device_option().device_type(), op2.device_option().device_type());
    ArgumentHelper helper1(op1);
    ArgumentHelper helper2(op2);
    for (auto& arg : op1.arg()) {
      const auto& name = arg.name();
      if (name == "net_pos") {
        continue;
      }
      CHECK(helper2.HasArgument(name))
          << "Argument " << name << " doesn't exist";
      const auto arg1 = helper1.GetSingleArgument<int>(name, 0);
      const auto arg2 = helper2.GetSingleArgument<int>(name, 0);
      TORCH_CHECK_EQ(arg1, arg2);
    }
  }
}

void checkShapeInfo(ShapeInfoMap& shape_map, ShapeInfoMap& expected_shape_map) {
  TORCH_CHECK_EQ(shape_map.size(), expected_shape_map.size());
  for (auto& [name, shape] : shape_map) {
    auto it = expected_shape_map.find(name);
    CHECK(it != expected_shape_map.end()) << "Didn't find name " << name;
    auto& expected_shape = it->second;
    EXPECT_EQ(expected_shape.getDimType(), shape.getDimType());
    ASSERT_EQ(expected_shape.shape.dims_size(), shape.shape.dims_size());
    for (int i = 0; i < shape.shape.dims_size(); ++i) {
      EXPECT_EQ(expected_shape.shape.dims(i), shape.shape.dims(i));
    }
    EXPECT_EQ(expected_shape.shape.data_type(), shape.shape.data_type()) << "Shapes don't match for " << name;
    EXPECT_EQ(expected_shape.is_quantized, shape.is_quantized);
  }
}

ShapeInfo makeTensorInfo(
    const std::vector<TensorBoundShape::DimType>& t,
    const std::vector<int64_t>& dims,
    TensorProto::DataType dtype = TensorProto_DataType_FLOAT) {
  ShapeInfo info;
  info.setDimType(t);
  TensorShape& shape = info.shape;
  for (const auto d : dims) {
    shape.add_dims(d);
  }
  shape.set_data_type(dtype);
  return info;
}

TEST(InBatchBroadcast, main) {
  NetDef net;
  net.add_op()->CopyFrom(
      CreateOperatorDef("FloatToHalf", "", {"blob"}, {"blob_half"}, {}));
  ShapeInfoMap shape_map;
  shape_map.emplace(
      "blob",
      makeTensorInfo(
          {TensorBoundShape_DimType_BATCH, TensorBoundShape_DimType_CONSTANT},
          {32, 16}));
  std::unordered_set<std::string> transform_blob({"blob"});
  opt::inBatchBroadcast(&net, transform_blob, 32, shape_map);
  NetDef expected_net;
  auto* op1 = expected_net.add_op();
  op1->CopyFrom(CreateOperatorDef(
      "FloatToHalf",
      "",
      {"blob"},
      {"blob_fp16"},
      {}));
  op1->mutable_device_option()->set_device_type(caffe2::PROTO_CPU);
  auto* op2 = expected_net.add_op();
  op2->CopyFrom(CreateOperatorDef(
      "HalfToFloat",
      "",
      {"blob_fp16"},
      {"blob_fp32"},
      {}));
  op2->mutable_device_option()->set_device_type(caffe2::PROTO_CPU);
  auto op3 = expected_net.add_op();
  op3->CopyFrom(CreateOperatorDef(
      "Tile",
      "",
      {"blob_fp32"},
      {"blob_tile"},
      {MakeArgument<int>("tiles", 32),
       MakeArgument<int>("axis", 0),
       MakeArgument<int>("dynamic", 1)}));
  op3->mutable_device_option()->set_device_type(caffe2::PROTO_CPU);
  auto op4 = expected_net.add_op();
  op4->CopyFrom(
      CreateOperatorDef("FloatToHalf", "", {"blob_tile"}, {"blob_half"}, {}));
  op4->mutable_device_option()->set_device_type(caffe2::PROTO_CPU);
  ShapeInfoMap expected_shape_map;
  expected_shape_map.emplace(
      "blob",
      makeTensorInfo(
          {TensorBoundShape_DimType_CONSTANT,
           TensorBoundShape_DimType_CONSTANT},
          {1, 16}));
  expected_shape_map.emplace(
      "blob_fp16",
      makeTensorInfo(
          {TensorBoundShape_DimType_CONSTANT,
           TensorBoundShape_DimType_CONSTANT},
          {1, 16},
          TensorProto_DataType_FLOAT16));
  expected_shape_map.emplace(
      "blob_fp32",
      makeTensorInfo(
          {TensorBoundShape_DimType_CONSTANT,
           TensorBoundShape_DimType_CONSTANT},
          {1, 16}));
  expected_shape_map.emplace(
      "blob_tile",
      makeTensorInfo(
          {TensorBoundShape_DimType_BATCH, TensorBoundShape_DimType_CONSTANT},
          {32, 16}));
  checkNet(net, expected_net);
  checkShapeInfo(shape_map, expected_shape_map);
}

TEST(InBatchBroadcast, fuse8bit) {
  NetDef net;
  net.add_op()->CopyFrom(CreateOperatorDef(
      "Fused8BitRowwiseQuantizedToFloat", "", {"blob_int8"}, {"blob"}, {}));
  ShapeInfoMap shape_map;
  shape_map.emplace(
      "blob_int8",
      makeTensorInfo(
          {TensorBoundShape_DimType_BATCH, TensorBoundShape_DimType_CONSTANT},
          {32, 24},
          TensorProto_DataType_UINT8));
  shape_map.emplace(
      "blob",
      makeTensorInfo(
          {TensorBoundShape_DimType_BATCH, TensorBoundShape_DimType_CONSTANT},
          {32, 16}));
  std::unordered_set<std::string> transform_blob({"blob_int8"});
  opt::inBatchBroadcast(&net, transform_blob, 32, shape_map);
  NetDef expected_net;
  auto* op1 = expected_net.add_op();
  op1->CopyFrom(CreateOperatorDef(
      "Fused8BitRowwiseQuantizedToFloat", "", {"blob_int8"}, {"blob"}, {}));
  op1->mutable_device_option()->set_device_type(caffe2::PROTO_CPU);
  auto* op2 = expected_net.add_op();
  op2->CopyFrom(CreateOperatorDef(
      "FloatToHalf",
      "",
      {"blob"},
      {"blob_fp16"},
      {}));
  op2->mutable_device_option()->set_device_type(caffe2::PROTO_CPU);
  auto* op3 = expected_net.add_op();
  op3->CopyFrom(CreateOperatorDef(
      "HalfToFloat",
      "",
      {"blob_fp16"},
      {"blob_fp32"},
      {}));
  op3->mutable_device_option()->set_device_type(caffe2::PROTO_CPU);
  auto* op4 = expected_net.add_op();
  op4->CopyFrom(CreateOperatorDef(
      "Tile",
      "",
      {"blob_fp32"},
      {"blob_tile"},
      {MakeArgument<int>("tiles", 32),
       MakeArgument<int>("axis", 0),
       MakeArgument<int>("dynamic", 1)}));
  op4->mutable_device_option()->set_device_type(caffe2::PROTO_CPU);
  ShapeInfoMap expected_shape_map;
  expected_shape_map.emplace(
      "blob_int8",
      makeTensorInfo(
          {TensorBoundShape_DimType_CONSTANT,
           TensorBoundShape_DimType_CONSTANT},
          {1, 24},
          TensorProto_DataType_UINT8));
  expected_shape_map.emplace(
      "blob",
      makeTensorInfo(
          {TensorBoundShape_DimType_CONSTANT,
           TensorBoundShape_DimType_CONSTANT},
          {1, 16}));
  expected_shape_map.emplace(
      "blob_fp16",
      makeTensorInfo(
          {TensorBoundShape_DimType_CONSTANT,
           TensorBoundShape_DimType_CONSTANT},
          {1, 16},
          TensorProto_DataType_FLOAT16));
  expected_shape_map.emplace(
      "blob_fp32",
      makeTensorInfo(
          {TensorBoundShape_DimType_CONSTANT,
           TensorBoundShape_DimType_CONSTANT},
          {1, 16},
          TensorProto_DataType_FLOAT));
  expected_shape_map.emplace(
      "blob_tile",
      makeTensorInfo(
          {TensorBoundShape_DimType_BATCH, TensorBoundShape_DimType_CONSTANT},
          {32, 16}));
  checkNet(net, expected_net);
  checkShapeInfo(shape_map, expected_shape_map);
}
} // namespace