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.

Context.Config.cs 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using Google.Protobuf;
  14. using System;
  15. using System.Diagnostics;
  16. using System.Linq;
  17. using Tensorflow.Common.Extensions;
  18. namespace Tensorflow.Contexts
  19. {
  20. /// <summary>
  21. /// Environment in which eager operations execute.
  22. /// </summary>
  23. public sealed partial class Context
  24. {
  25. protected Device.PhysicalDevice[] _physical_devices;
  26. protected Dictionary<Device.PhysicalDevice, int> _physical_device_to_index;
  27. ConfigProto _config;
  28. public ConfigProto Config
  29. {
  30. get
  31. {
  32. _initialize_physical_devices();
  33. var config = new ConfigProto();
  34. if(_config is not null)
  35. {
  36. config.MergeFrom(_config);
  37. }
  38. config.LogDevicePlacement = _log_device_placement;
  39. config.DeviceCount["CPU"] = 0;
  40. config.DeviceCount["GPU"] = 0;
  41. foreach(var dev in _physical_devices)
  42. {
  43. if (config.DeviceCount.ContainsKey(dev.DeviceType))
  44. {
  45. config.DeviceCount[dev.DeviceType] += 1;
  46. }
  47. else
  48. {
  49. config.DeviceCount[dev.DeviceType] = 1;
  50. }
  51. }
  52. var gpu_options = _compute_gpu_options();
  53. config.GpuOptions = GPUOptions.Parser.ParseFrom(gpu_options.ToByteArray());
  54. return config;
  55. }
  56. set
  57. {
  58. _config = value;
  59. }
  60. }
  61. protected void _initialize_physical_devices(bool reinitialize = false)
  62. {
  63. if(!reinitialize && _physical_devices is not null)
  64. {
  65. return;
  66. }
  67. var devs = list_physical_devices();
  68. _physical_devices = devs.Select(d => new Device.PhysicalDevice()
  69. {
  70. DeviceName = d.DeviceName,
  71. DeviceType = d.DeviceType
  72. }).ToArray();
  73. _physical_device_to_index = _physical_devices.Select((p, i) => new KeyValuePair<Device.PhysicalDevice, int>(p, i))
  74. .ToDictionary(x => x.Key, x => x.Value);
  75. _import_config();
  76. }
  77. protected void _import_config()
  78. {
  79. if(_config is null)
  80. {
  81. return;
  82. }
  83. if(!_config.DeviceCount.TryGetValue("CPU", out var num_cpus))
  84. {
  85. num_cpus = 1;
  86. }
  87. if(num_cpus != 1)
  88. {
  89. // TODO(Rinne): implement it.
  90. }
  91. var gpus = _physical_devices.Where(d => d.DeviceType == "GPU");
  92. if(gpus.Count() == 0)
  93. {
  94. return;
  95. }
  96. if(!_config.DeviceCount.TryGetValue("GPU", out var gpu_count))
  97. {
  98. gpu_count = 0;
  99. }
  100. // TODO(Rinne): implement it.
  101. }
  102. ConfigProto MergeConfig()
  103. {
  104. Config.LogDevicePlacement = _log_device_placement;
  105. // var gpu_options = _compute_gpu_options();
  106. // Config.GpuOptions.AllowGrowth = gpu_options.AllowGrowth;
  107. return Config;
  108. }
  109. GPUOptions _compute_gpu_options()
  110. {
  111. // By default, TensorFlow maps nearly all of the GPU memory of all GPUs
  112. // https://www.tensorflow.org/guide/gpu
  113. return new GPUOptions()
  114. {
  115. AllowGrowth = get_memory_growth("GPU")
  116. };
  117. }
  118. }
  119. }