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

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