MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / TFE_Py_EncodeTensor

Function TFE_Py_EncodeTensor

tensorflow/python/eager/pywrap_tfe_src.cc:3283–3359  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3281};
3282
3283tensorflow::Status TFE_Py_EncodeTensor(PyObject* arg,
3284 bool include_tensor_ranks_only,
3285 EncodeResult* result) {
3286 if (EagerTensor_CheckExact(arg)) {
3287 TFE_TensorHandle* t = EagerTensor_Handle(arg);
3288 tensorflow::TensorShape tensor_shape;
3289 TF_RETURN_IF_ERROR(t->handle->Shape(&tensor_shape));
3290
3291 absl::StrAppend(&result->str, kDType, t->handle->dtype);
3292
3293 absl::StrAppend(&result->str, kShape);
3294 if (include_tensor_ranks_only) {
3295 absl::StrAppend(&result->str, tensor_shape.dim_sizes().size());
3296 } else {
3297 for (tensorflow::int64 dim_size : tensor_shape.dim_sizes()) {
3298 absl::StrAppend(&result->str, dim_size, kShapeDelim);
3299 }
3300 }
3301 return tensorflow::Status::OK();
3302 }
3303
3304 tensorflow::Safe_PyObjectPtr dtype_object(
3305 PyObject_GetAttrString(arg, "dtype"));
3306
3307 if (dtype_object == nullptr) {
3308 return tensorflow::errors::InvalidArgument(
3309 "ops.Tensor object doesn't have dtype() attr.");
3310 }
3311
3312 tensorflow::Safe_PyObjectPtr dtype_enum(
3313 PyObject_GetAttrString(dtype_object.get(), "_type_enum"));
3314
3315 if (dtype_enum == nullptr) {
3316 return tensorflow::errors::InvalidArgument(
3317 "ops.Tensor's dtype object doesn't have _type_enum() attr.");
3318 }
3319
3320 tensorflow::DataType dtype =
3321 static_cast<tensorflow::DataType>(MakeInt(dtype_enum.get()));
3322
3323 absl::StrAppend(&result->str, kDType, dtype);
3324
3325 static char _shape_tuple[] = "_shape_tuple";
3326 tensorflow::Safe_PyObjectPtr shape_tuple(
3327 PyObject_CallMethod(arg, _shape_tuple, nullptr));
3328
3329 if (shape_tuple == nullptr) {
3330 return tensorflow::errors::InvalidArgument(
3331 "ops.Tensor object doesn't have _shape_tuple() method.");
3332 }
3333
3334 if (shape_tuple.get() == Py_None) {
3335 // Unknown shape, encode that directly.
3336 absl::StrAppend(&result->str, kNone);
3337 return tensorflow::Status::OK();
3338 }
3339
3340 absl::StrAppend(&result->str, kShape);

Callers 1

TFE_Py_EncodeArgHelperFunction · 0.85

Calls 7

InvalidArgumentFunction · 0.85
MakeIntFunction · 0.85
StrAppendFunction · 0.50
ShapeMethod · 0.45
sizeMethod · 0.45
dim_sizesMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected