提交 de08b763 authored 作者: Iban Harlouchet's avatar Iban Harlouchet

flake8 of theano/gof/cc.py

上级 11a78c73
"""
Defines Linkers that deal with C implementations.
"""
from __future__ import print_function
# Python imports
from copy import copy
import os
import sys
from theano.compat import izip
import logging
import numpy
import theano
from theano import config
from theano.compat import PY3
from theano.compat import izip
from six import string_types, reraise
from six.moves import StringIO, xrange
from theano.gof.utils import MethodNotDefined
import theano
from theano import config
# Note that we need to do this before importing cutils, since when there is
# no theano cache dir initialized yet, importing cutils may require compilation
# of cutils_ext.
from theano.configparser import AddConfigVar, StrParam
# gof imports
from theano.gof import graph
from theano.gof import link
from theano.gof import utils
from theano.gof import cmodule
from theano.gof.compilelock import get_lock, release_lock
from theano.gof.callcache import CallCache
AddConfigVar('gcc.cxxflags',
"Extra compiler flags for gcc",
StrParam(""))
if PY3:
import hashlib
......@@ -41,35 +58,14 @@ else:
assert isinstance(msg, numpy.ndarray)
return hashlib.md5(numpy.getbuffer(msg)).hexdigest()
_logger = logging.getLogger("theano.gof.cc")
def hash_from_file(file_path):
"""Return the MD5 hash of a file."""
return hash_from_code(open(file_path, 'rb').read())
# Note that we need to do this before importing cutils, since when there is
# no theano cache dir initialized yet, importing cutils may require compilation
# of cutils_ext.
from theano.configparser import AddConfigVar, StrParam
AddConfigVar('gcc.cxxflags',
"Extra compiler flags for gcc",
StrParam(""))
# gof imports
from theano.gof import graph
from theano.gof import link
from theano.gof import utils
from theano.gof.compilelock import get_lock, release_lock
from theano.gof import cmodule
import logging
_logger = logging.getLogger("theano.gof.cc")
from theano.gof.callcache import CallCache
run_cthunk = None # Will be imported only when needed.
......@@ -314,9 +310,8 @@ def get_c_declare(r, name, sub):
"""Wrapper around c_declare that declares py_name"""
if any([c != "output" and getattr(c.op, 'check_input',
config.check_input) for (c, _) in r.clients]) or (r.owner
and getattr(r.owner.op, 'check_input', True)):
config.check_input) for (c, _) in r.clients]) or (
r.owner and getattr(r.owner.op, 'check_input', True)):
c_declare = r.type.c_declare(name, sub, True)
else:
c_declare = r.type.c_declare(name, sub, False)
......@@ -532,7 +527,7 @@ class CLinker(link.Linker):
if isinstance(r, graph.Constant) and
r not in self.inputs)
self.temps = list(set(self.variables).difference(
self.inputs).difference(self.outputs).difference(self.orphans))
self.inputs).difference(self.outputs).difference(self.orphans))
self.consts = []
def code_gen(self):
......@@ -821,7 +816,7 @@ class CLinker(link.Linker):
ret = []
# generic support code
for x in [y.type for y in self.variables] + [
y.op for y in self.node_order]:
y.op for y in self.node_order]:
try:
ret.append(x.c_support_code())
except utils.MethodNotDefined:
......@@ -840,11 +835,11 @@ class CLinker(link.Linker):
# FillMissing must disable some of them. Putting -ffast-math would
# make it disable all other parameter at the same time.
ret += ["-fno-math-errno",
#"-funsafe-math-optimizations",
#"-fno-signaling-nans",
#"-fcx-limited-range",
#"-fno-rounding-math",
#"-ffinite-math-only",
# "-funsafe-math-optimizations",
# "-fno-signaling-nans",
# "-fcx-limited-range",
# "-fno-rounding-math",
# "-ffinite-math-only",
# the current code generate label event if they are not used.
# Could use gcc attribute for those label only
......@@ -853,7 +848,7 @@ class CLinker(link.Linker):
"-Wno-write-strings", # generated by our code generator...
]
for x in [y.type for y in self.variables] + [
y.op for y in self.node_order]:
y.op for y in self.node_order]:
try:
ret += x.c_compile_args()
except utils.MethodNotDefined:
......@@ -866,7 +861,7 @@ class CLinker(link.Linker):
# to reorder them
ret += c_compiler.compile_args()
for x in [y.type for y in self.variables] + [
y.op for y in self.node_order]:
y.op for y in self.node_order]:
try:
for i in x.c_no_compile_args():
try:
......@@ -886,7 +881,7 @@ class CLinker(link.Linker):
"""
ret = []
for x in [y.type for y in self.variables] + [
y.op for y in self.node_order]:
y.op for y in self.node_order]:
try:
ret += x.c_headers()
except utils.MethodNotDefined:
......@@ -901,7 +896,7 @@ class CLinker(link.Linker):
"""
ret = []
for x in [y.type for y in self.variables] + [
y.op for y in self.node_order]:
y.op for y in self.node_order]:
try:
ret += x.c_init_code()
except utils.MethodNotDefined:
......@@ -911,7 +906,7 @@ class CLinker(link.Linker):
def c_compiler(self):
c_compiler = None
for x in [y.type for y in self.variables] + [
y.op for y in self.node_order]:
y.op for y in self.node_order]:
if hasattr(x, 'c_compiler'):
x_compiler = x.c_compiler()
else:
......@@ -938,7 +933,7 @@ class CLinker(link.Linker):
"""
ret = []
for x in [y.type for y in self.variables] + [
y.op for y in self.node_order]:
y.op for y in self.node_order]:
try:
ret += x.c_header_dirs()
except utils.MethodNotDefined:
......@@ -954,7 +949,7 @@ class CLinker(link.Linker):
"""
ret = []
for x in [y.type for y in self.variables] + [
y.op for y in self.node_order]:
y.op for y in self.node_order]:
try:
ret += x.c_libraries()
except utils.MethodNotDefined:
......@@ -970,7 +965,7 @@ class CLinker(link.Linker):
"""
ret = []
for x in [y.type for y in self.variables] + [
y.op for y in self.node_order]:
y.op for y in self.node_order]:
try:
ret += x.c_lib_dirs()
except utils.MethodNotDefined:
......@@ -1150,7 +1145,7 @@ class CLinker(link.Linker):
libraries=self.libraries(),
header_dirs=self.header_dirs(),
c_compiler=self.c_compiler(),
)
)
def cmodule_key_(self, fgraph, no_recycling, compile_args=None,
libraries=None, header_dirs=None, insert_config_md5=True,
......@@ -1335,7 +1330,6 @@ class CLinker(link.Linker):
preargs.remove('-DREPLACE_WITH_AMDLIBM')
if 'amdlibm' in libs:
libs.remove('amdlibm')
src_code = mod.code()
get_lock()
try:
_logger.debug("LOCATION %s", str(location))
......@@ -1371,9 +1365,9 @@ class CLinker(link.Linker):
code = self.instantiate_code(1 + len(self.args))
instantiate = cmodule.ExtFunction('instantiate', code,
method=cmodule.METH_VARARGS)
#['error_storage'] + argnames,
#local_dict = d,
# global_dict = {})
# ['error_storage'] + argnames,
# local_dict = d,
# global_dict = {})
# Static methods that can run and destroy the struct built by
# instantiate.
......@@ -1498,7 +1492,7 @@ class _CThunk(object):
global run_cthunk
if run_cthunk is None:
# Lazy import to avoid compilation when importing theano.
from theano.gof.cutils import run_cthunk
from theano.gof.cutils import run_cthunk # noqa
self.cthunk = cthunk
self.init_tasks = init_tasks
self.tasks = tasks
......@@ -1534,7 +1528,8 @@ class _CThunk(object):
exc_value.__thunk_trace__ = trace
except Exception:
print(('ERROR retrieving error_storage.'
' Was the error set in the c code?'), end=' ', file=sys.stderr)
'Was the error set in the c code?'),
end=' ', file=sys.stderr)
print(self.error_storage, file=sys.stderr)
raise
reraise(exc_type, exc_value, exc_trace)
......@@ -1641,11 +1636,11 @@ class OpWiseCLinker(link.LocalLinker):
for node in order:
if self.allow_gc:
post_thunk_old_storage.append([storage_map[input]
for input in node.inputs
if ((input in computed) and
(input not in fgraph.outputs) and
node == last_user[input])])
post_thunk_old_storage.append(
[storage_map[input] for input in node.inputs
if ((input in computed) and
(input not in fgraph.outputs) and
node == last_user[input])])
if no_recycling is True:
no_recycling = list(storage_map.values())
......@@ -1741,12 +1736,12 @@ class DualLinker(link.Linker):
no_recycling = self.no_recycling
_f, i1, o1, thunks1, order1 = (
link.PerformLinker(schedule=self.schedule).accept(fgraph,
no_recycling=no_recycling).make_all(**kwargs))
link.PerformLinker(schedule=self.schedule).accept(
fgraph, no_recycling=no_recycling).make_all(**kwargs))
kwargs.pop('input_storage', None)
_f, i2, o2, thunks2, order2 = (
OpWiseCLinker(schedule=self.schedule).accept(fgraph,
no_recycling=no_recycling).make_all(**kwargs))
OpWiseCLinker(schedule=self.schedule).accept(
fgraph, no_recycling=no_recycling).make_all(**kwargs))
def f():
for input1, input2 in izip(i1, i2):
......
......@@ -244,7 +244,6 @@ whitelist_flake8 = [
"gof/unify.py",
"gof/graph.py",
"gof/__init__.py",
"gof/cc.py",
"gof/opt.py",
"gof/link.py",
"gof/fg.py",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论