tensorflow/core/kernels/save_op_test.cc
// Here we save only slices.  We restore them in a larger tensor and we check
// that the right slice is restored.  It is quite tricky to check that the
// right slices are actually restored so instead we just check that
// CopySliceData() return true/false depending on the slice we ask for.
Here we save only slices.  We restore them in a larger tensor and we check
that the right slice is restored.  It is quite tricky to check that the
right slices are actually restored so instead we just check that
CopySliceData() return true/false depending on the slice we ask for.
Here we save only slices . We restore them in a larger tensor and we check that the right slice is restored . It is quite tricky to check that the right slices are actually restored so instead we just check that CopySliceData () return true / false depending on the slice we ask for .
TEST_F(SaveSlicesOpTest, Slices) {
  const string filename = io::JoinPath(testing::TmpDir(), "tensor_slices");
  const string tensornames[] = {"tensor_int", "tensor_float", "tensor_double",
                                "tensor_qint8", "tensor_qint32"};
  const string tensorshapes[] = {
      "10 -",         
      "2 4 -:0,2",    
      "2 4 0,1:2,2",  
      "3 2 -:-",      
      "2 3 1,1:2,1"   
  };
  MakeOp();
  AddInput<tstring>(TensorShape({}),
                    [&filename](int x) -> tstring { return filename; });
  AddInput<tstring>(TensorShape({5}), [&tensornames](int x) -> tstring {
    return tensornames[x];
  });
  AddInput<tstring>(TensorShape({5}), [&tensorshapes](int x) -> tstring {
    return tensorshapes[x];
  });
  AddInput<int32>(TensorShape({10}), [](int x) -> int32 { return x + 1; });
  AddInput<float>(TensorShape({2, 2}),
                  [](int x) -> float { return static_cast<float>(x) / 10; });
  AddInput<double>(TensorShape({1, 2}),
                   [](int x) -> double { return static_cast<double>(x) / 20; });
  AddInput<qint8>(TensorShape({3, 2}),
                  [](int x) -> qint8 { return *reinterpret_cast<qint8*>(&x); });
  AddInput<qint32>(TensorShape({1, 1}), [](int x) -> qint32 {
    return *reinterpret_cast<qint32*>(&x) * qint8(2);
  });
  TF_ASSERT_OK(RunOpKernel());
  checkpoint::TensorSliceReader reader(filename,
                                       checkpoint::OpenTableTensorSliceReader);
  TF_EXPECT_OK(reader.status());
  {
    TensorShape shape;
    DataType type;
    EXPECT_TRUE(reader.HasTensor("tensor_int", &shape, &type));
    TensorShape expected({10});
    EXPECT_TRUE(shape.IsSameSize(expected));
    EXPECT_EQ(DT_INT32, type);
    TensorSlice s = TensorSlice::ParseOrDie("-");
    int data[10];
    EXPECT_TRUE(reader.CopySliceData("tensor_int", s, data));
  }
  {
    TensorShape shape;
    DataType type;
    EXPECT_TRUE(reader.HasTensor("tensor_float", &shape, &type));
    TensorShape expected({2, 4});
    EXPECT_TRUE(shape.IsSameSize(expected));
    EXPECT_EQ(DT_FLOAT, type);
    TensorSlice full_slice = TensorSlice::ParseOrDie("-:-");
    TensorSlice saved_slice = TensorSlice::ParseOrDie("-:0,2");
    float data[8];
    EXPECT_FALSE(reader.CopySliceData("tensor_float", full_slice, data));
    EXPECT_TRUE(reader.CopySliceData("tensor_float", saved_slice, data));
  }
  {
    TensorShape shape;
    DataType type;
    EXPECT_TRUE(reader.HasTensor("tensor_double", &shape, &type));
    TensorShape expected({2, 4});
    EXPECT_TRUE(shape.IsSameSize(expected));
    EXPECT_EQ(DT_DOUBLE, type);
    TensorSlice full_slice = TensorSlice::ParseOrDie("-:-");
    TensorSlice saved_slice = TensorSlice::ParseOrDie("0,1:2,2");
    double data[8];
    EXPECT_FALSE(reader.CopySliceData("tensor_double", full_slice, data));
    EXPECT_TRUE(reader.CopySliceData("tensor_double", saved_slice, data));
  }
  {
    TensorShape shape;
    DataType type;
    EXPECT_TRUE(reader.HasTensor("tensor_qint8", &shape, &type));
    TensorShape expected({3, 2});
    EXPECT_TRUE(shape.IsSameSize(expected));
    EXPECT_EQ(DT_QINT8, type);
    TensorSlice s = TensorSlice::ParseOrDie("-:-");
    qint8 data[6];
    EXPECT_TRUE(reader.CopySliceData("tensor_qint8", s, data));
  }
  {
    TensorShape shape;
    DataType type;
    EXPECT_TRUE(reader.HasTensor("tensor_qint32", &shape, &type));
    TensorShape expected({2, 3});
    EXPECT_TRUE(shape.IsSameSize(expected));
    EXPECT_EQ(DT_QINT32, type);
    TensorSlice s = TensorSlice::ParseOrDie("1,1:2,1");
    TensorSlice full_slice = TensorSlice::ParseOrDie("-:-");
    TensorSlice saved_slice = TensorSlice::ParseOrDie("1,1:2,1");
    qint32 data[6];
    EXPECT_FALSE(reader.CopySliceData("tensor_qint32", full_slice, data));
    EXPECT_TRUE(reader.CopySliceData("tensor_qint32", saved_slice, data));
  }
}
TEST_F ( SaveSlicesOpTest , Slices ) { const string filename = io :: JoinPath ( testing :: TmpDir ( ) , " " ) ; const string tensornames [ ] = { " " , " " , " " , " " , " " } ; const string tensorshapes [ ] = { " " , " " , " " , " " , " " } ; MakeOp ( ) ; AddInput < tstring > ( TensorShape ( { } ) , [ & filename ] ( int x ) -> tstring { return filename ; } ) ; AddInput < tstring > ( TensorShape ( { 5 } ) , [ & tensornames ] ( int x ) -> tstring { return tensornames [ x ] ; } ) ; AddInput < tstring > ( TensorShape ( { 5 } ) , [ & tensorshapes ] ( int x ) -> tstring { return tensorshapes [ x ] ; } ) ; AddInput < int32 > ( TensorShape ( { 10 } ) , [ ] ( int x ) -> int32 { return x + 1 ; } ) ; AddInput < float > ( TensorShape ( { 2 , 2 } ) , [ ] ( int x ) -> float { return static_cast < float > ( x ) / 10 ; } ) ; AddInput < double > ( TensorShape ( { 1 , 2 } ) , [ ] ( int x ) -> double { return static_cast < double > ( x ) / 20 ; } ) ; AddInput < qint8 > ( TensorShape ( { 3 , 2 } ) , [ ] ( int x ) -> qint8 { return * reinterpret_cast < qint8 * > ( & x ) ; } ) ; AddInput < qint32 > ( TensorShape ( { 1 , 1 } ) , [ ] ( int x ) -> qint32 { return * reinterpret_cast < qint32 * > ( & x ) * qint8 ( 2 ) ; } ) ; TF_ASSERT_OK ( RunOpKernel ( ) ) ; checkpoint :: TensorSliceReader reader ( filename , checkpoint :: OpenTableTensorSliceReader ) ; TF_EXPECT_OK ( reader . status ( ) ) ; { TensorShape shape ; DataType type ; EXPECT_TRUE ( reader . HasTensor ( " " , & shape , & type ) ) ; TensorShape expected ( { 10 } ) ; EXPECT_TRUE ( shape . IsSameSize ( expected ) ) ; EXPECT_EQ ( DT_INT32 , type ) ; TensorSlice s = TensorSlice :: ParseOrDie ( " " ) ; int data [ 10 ] ; EXPECT_TRUE ( reader . CopySliceData ( " " , s , data ) ) ; } { TensorShape shape ; DataType type ; EXPECT_TRUE ( reader . HasTensor ( " " , & shape , & type ) ) ; TensorShape expected ( { 2 , 4 } ) ; EXPECT_TRUE ( shape . IsSameSize ( expected ) ) ; EXPECT_EQ ( DT_FLOAT , type ) ; TensorSlice full_slice = TensorSlice :: ParseOrDie ( " " ) ; TensorSlice saved_slice = TensorSlice :: ParseOrDie ( " " ) ; float data [ 8 ] ; EXPECT_FALSE ( reader . CopySliceData ( " " , full_slice , data ) ) ; EXPECT_TRUE ( reader . CopySliceData ( " " , saved_slice , data ) ) ; } { TensorShape shape ; DataType type ; EXPECT_TRUE ( reader . HasTensor ( " " , & shape , & type ) ) ; TensorShape expected ( { 2 , 4 } ) ; EXPECT_TRUE ( shape . IsSameSize ( expected ) ) ; EXPECT_EQ ( DT_DOUBLE , type ) ; TensorSlice full_slice = TensorSlice :: ParseOrDie ( " " ) ; TensorSlice saved_slice = TensorSlice :: ParseOrDie ( " " ) ; double data [ 8 ] ; EXPECT_FALSE ( reader . CopySliceData ( " " , full_slice , data ) ) ; EXPECT_TRUE ( reader . CopySliceData ( " " , saved_slice , data ) ) ; } { TensorShape shape ; DataType type ; EXPECT_TRUE ( reader . HasTensor ( " " , & shape , & type ) ) ; TensorShape expected ( { 3 , 2 } ) ; EXPECT_TRUE ( shape . IsSameSize ( expected ) ) ; EXPECT_EQ ( DT_QINT8 , type ) ; TensorSlice s = TensorSlice :: ParseOrDie ( " " ) ; qint8 data [ 6 ] ; EXPECT_TRUE ( reader . CopySliceData ( " " , s , data ) ) ; } { TensorShape shape ; DataType type ; EXPECT_TRUE ( reader . HasTensor ( " " , & shape , & type ) ) ; TensorShape expected ( { 2 , 3 } ) ; EXPECT_TRUE ( shape . IsSameSize ( expected ) ) ; EXPECT_EQ ( DT_QINT32 , type ) ; TensorSlice s = TensorSlice :: ParseOrDie ( " " ) ; TensorSlice full_slice = TensorSlice :: ParseOrDie ( " " ) ; TensorSlice saved_slice = TensorSlice :: ParseOrDie ( " " ) ; qint32 data [ 6 ] ; EXPECT_FALSE ( reader . CopySliceData ( " " , full_slice , data ) ) ; EXPECT_TRUE ( reader . CopySliceData ( " " , saved_slice , data ) ) ; } }
Here we save only slices.
Here we save only slices .
// Specifies that the data we save are slices of larger tensors. // See core/framework/tensor_slice.h for the slice syntax. // Full contents of a 10 element vector. // A 2x2 slice of a 2x4 tensor. // A 1x2 slice of a 2x4 tensor. // Full contents of a 3x2 tensor. // Another 1x1 slice of a2x3 tensor. // Add a file name // Add the tensor names // Add the tensor shapes and slices // Add a 1-d integer tensor // Add a 2-d float tensor // Add a 2-d double tensor // Add a 2-d qint8 tensor // Add a 2-d qint32 tensor // Check that the checkpoint file is properly written // We expect to find all saved tensors // The 1-d integer tensor // We saved the full tensor so we should be able to read it all. // The 2-d float tensor // We saved the slice "-:0,2" so we should not be able to read the full // tensor. // The 2-d double tensor // We saved the slice "0,1:2,2" so we should not be able to read the full // tensor. // The 2-d qint8 tensor // We saved the full slice. // The 2-d qint32 tensor // We expect the tensor value to be correct.