| 218 | |
| 219 | # use thread local to create thread save global variables. |
| 220 | class GlobalThreadLocal(threading.local): |
| 221 | def __init__(self): |
| 222 | """ |
| 223 | init the thread local data. |
| 224 | TODO(xiongkun): how to access another thread local data ? |
| 225 | """ |
| 226 | global _dygraph_tracer_ |
| 227 | self._in_to_static_mode_ = False |
| 228 | self._functional_dygraph_context_manager = None |
| 229 | self._dygraph_tracer_ = _dygraph_tracer_ |
| 230 | env_pir_enabled = os.environ.get("FLAGS_enable_pir_api") |
| 231 | |
| 232 | if env_pir_enabled is not None: |
| 233 | pir_enabled = env_pir_enabled.lower() not in [ |
| 234 | 'n', |
| 235 | 'no', |
| 236 | 'f', |
| 237 | 'false', |
| 238 | 'off', |
| 239 | '0', |
| 240 | ] |
| 241 | set_flags({"FLAGS_enable_pir_api": pir_enabled}) |
| 242 | self._use_pir_api_ = get_flags("FLAGS_enable_pir_api")[ |
| 243 | "FLAGS_enable_pir_api" |
| 244 | ] |
| 245 | |
| 246 | def __str__(self): |
| 247 | strings = [] |
| 248 | strings.append("_in_to_static_mode_:" + str(self._in_to_static_mode_)) |
| 249 | strings.append( |
| 250 | "_functional_dygraph_context_manager:" |
| 251 | + str(self._functional_dygraph_context_manager) |
| 252 | ) |
| 253 | strings.append("_dygraph_tracer_:" + str(self._dygraph_tracer_)) |
| 254 | return "\n".join(strings) |
| 255 | |
| 256 | def __setattr__(self, name, val): |
| 257 | if name == "_dygraph_tracer_": |
| 258 | global _dygraph_tracer_ |
| 259 | _dygraph_tracer_ = val |
| 260 | core._switch_tracer(val) |
| 261 | self.__dict__[name] = val |
| 262 | |
| 263 | |
| 264 | _dygraph_tracer_ = None |