使用自定义数据类型创建张量流图时出错

时间:2018-07-12 08:44:08

标签: c++ tensorflow

根据this问题,我尝试将自定义类型minifloat添加到可用tf类型列表中。采取了以下步骤。

  • 在名称空间tensorflow class minifloat中使用接口和 等于struct bfloat16的免费函数。 Minifloat存储在 int32值。将标头添加到BUILD文件中。

  • types.proto添加了DT_MINIFLOAT。匹配的枚举DT_MINIDLOATtypes.h中输入类型minifloat。匹配的字符串和枚举 types.cc。已注册的新实数类型 register_types.h

  • type traits struct is_simple_type中添加到最小浮动。

  • numeric_types.h中添加了功能static tensorflow::minifloat FloatToMinifloat(float)struct NumTraits<tensorflow::minifloat>

  • 在[tensor.cc] [9]中指定的struct ProtoHelper<minifloat>由 函数BeginNumElementsFill的逻辑 minifloat存储在uint32中。

  • 在[constant_folding.cc] [10] CreateConstantTensorAttrValue中, ConstantFolding::IsOnesConstantFolding::IsZeros添加了切换 由DT_MINIFLOAT。在[fill_functor.cc] [11]中添加了 DEFINE_SETZERO_CPU(minifloat)DEFINE_SETONE_CPU(minifloat)

  • 在[utils] [12] SetTensorValue中添加了DT_MINIFLOAT进行的切换。

  • 定义了GetCpuCastFromMinifloat,从 [cast_op_impl.h] [13]和[cast_op.cc] [14],已注册的CPU内核,用于 浮空的。

  • 在[fill_functor.cc]中[11] RemoteFusedGraphExecuteUtils::CopyByteArrayToTensor添加了切换 通过DT_MINIFLOAT。

某些步骤可能是多余的。而且一些重要的步骤可能已经错过了。我试图创建并运行这样的图形:

using namespace tensorflow;
using namespace tensorflow::ops;
const int n = 1;
minifloat a_bf[n];
minifloat b_bf[n];
a_bf[0] = 1.345f;
b_bf[0] = 2.28f;

Tensor tensor1(DT_MINIFLOAT, TensorShape({ n }));
Tensor tensor2(DT_MINIFLOAT, TensorShape({ n }));
auto input_map1 = tensor1.tensor<minifloat, 1>();
auto input_map2 = tensor2.tensor<minifloat, 1>();
for (int i = 0; i < n; i++) {
    input_map1(i) = a_bf[i];
    input_map2(i) = b_bf[i];
}

Scope root = Scope::NewRootScope();
auto I = Const(root, tensor1);
auto W = Const(root, tensor2);
auto Mult = Multiply(root.WithOpName("mult"), I, W);
std::vector<Tensor> outputs;
ClientSession session(root);
TF_CHECK_OK(session.Run({Mult}, &outputs));

auto result_tensor = outputs[0].tensor<minifloat, 1>();
float result(result_tensor(0));
std::cout << result << std::endl;

此代码已成功编译,但由于未创建节点Mult(或包含空Op),因此在运行期间出现段错误。

有什么想法需要修正或添加,以便可以使用自定义数据类型运行图形吗?

0 个答案:

没有答案