You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CApiColocationTest.cs 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. namespace Tensorflow.Native.UnitTest
  5. {
  6. /// <summary>
  7. /// tensorflow\c\c_api_test.cc
  8. /// `class CApiColocationTest`
  9. /// </summary>
  10. [TestClass]
  11. public class CApiColocationTest : CApiTest, IDisposable
  12. {
  13. private Graph graph_ = new Graph();
  14. private Status s_ = new Status();
  15. private Operation feed1_;
  16. private Operation feed2_;
  17. private Operation constant_;
  18. private OperationDescription desc_;
  19. [TestInitialize]
  20. public void SetUp()
  21. {
  22. feed1_ = c_test_util.Placeholder(graph_, s_, "feed1");
  23. s_.Check();
  24. feed2_ = c_test_util.Placeholder(graph_, s_, "feed2");
  25. s_.Check();
  26. constant_ = c_test_util.ScalarConst(10, graph_, s_);
  27. s_.Check();
  28. desc_ = graph_.NewOperation("AddN", "add");
  29. TF_Output[] inputs = { new TF_Output(feed1_, 0), new TF_Output(constant_, 0) };
  30. desc_.AddInputList(inputs);
  31. }
  32. private void SetViaStringList(OperationDescription desc, string[] list)
  33. {
  34. var list_ptrs = new IntPtr[list.Length];
  35. var list_lens = new uint[list.Length];
  36. StringVectorToArrays(list, list_ptrs, list_lens);
  37. c_api.TF_SetAttrStringList(desc, "_class", list_ptrs, list_lens, list.Length);
  38. }
  39. private void StringVectorToArrays(string[] v, IntPtr[] ptrs, uint[] lens)
  40. {
  41. for (int i = 0; i < v.Length; ++i)
  42. {
  43. ptrs[i] = Marshal.StringToHGlobalAnsi(v[i]);
  44. lens[i] = (uint)v[i].Length;
  45. }
  46. }
  47. private void FinishAndVerify(OperationDescription desc, string[] expected)
  48. {
  49. var op = desc_.FinishOperation(s_);
  50. ASSERT_EQ(TF_Code.TF_OK, s_.Code);
  51. VerifyCollocation(op, expected);
  52. }
  53. private void VerifyCollocation(Operation op, string[] expected)
  54. {
  55. var handle = c_api.TF_OperationGetAttrMetadata(op, "_class", s_.Handle);
  56. TF_AttrMetadata m = new TF_AttrMetadata();
  57. if (expected.Length == 0)
  58. {
  59. ASSERT_EQ(TF_Code.TF_INVALID_ARGUMENT, s_.Code);
  60. EXPECT_EQ("Operation 'add' has no attr named '_class'.", s_.Message);
  61. return;
  62. }
  63. EXPECT_EQ(TF_Code.TF_OK, s_.Code);
  64. // EXPECT_EQ(1, m.is_list);
  65. // EXPECT_EQ(expected.Length, m.list_size);
  66. // EXPECT_EQ(TF_AttrType.TF_ATTR_STRING, m.type);
  67. string[] values = new string[expected.Length];
  68. uint[] lens = new uint[expected.Length];
  69. string[] storage = new string[m.total_size];
  70. //c_api.TF_OperationGetAttrStringList(op, "_class", values, lens, expected.Length, storage, m.total_size, s_);
  71. // EXPECT_EQ(TF_Code.TF_OK, s_.Code);
  72. for (int i = 0; i < expected.Length; ++i)
  73. {
  74. // EXPECT_EQ(expected[i], values[i] + lens[i]);
  75. }
  76. }
  77. [TestMethod]
  78. public void ColocateWith()
  79. {
  80. c_api.TF_ColocateWith(desc_, feed1_);
  81. FinishAndVerify(desc_, new string[] { "loc:@feed1" });
  82. }
  83. [TestMethod]
  84. public void StringList()
  85. {
  86. SetViaStringList(desc_, new string[] { "loc:@feed1" });
  87. FinishAndVerify(desc_, new string[] { "loc:@feed1" });
  88. }
  89. public void Dispose()
  90. {
  91. graph_.Dispose();
  92. s_.Dispose();
  93. }
  94. }
  95. }