第13章計算の性能
13.1.1. Symbolic Programming13.1.1 記号的プログラミング
Consider the alternative, symbolic programming, where computation is usually performed only once the process has been fully defined. This strategy is used by multiple deep learning frameworks, including Theano and TensorFlow (the latter has acquired imperative extensions). It usually involves the following steps:
英語のヒント
別の方法である記号的プログラミングを考えよう。そこでは、ふつう過程が完全に定義されてはじめて計算が行われる。この戦略は、TheanoやTensorFlow(後者は命令型の拡張を取り入れた)を含む、複数の深層学習のフレームワークで使われている。それはふつう次の段階からなる。
- Define the operations to be executed.
英語のヒント
実行する演算を定義する。 - Compile the operations into an executable program.
英語のヒント
演算をコンパイルして、実行できるプログラムにする。 - Provide the required inputs and call the compiled program for execution.
英語のヒント
必要な入力を与え、コンパイルしたプログラムを呼び出して実行する。
This allows for a significant amount of optimization. First, we can skip the Python interpreter in many cases, thus removing a performance bottleneck that can become significant on multiple fast GPUs paired with a single Python thread on a CPU. Second, a compiler might optimize and rewrite the above code into
print((1 + 2) + (3 + 4)) or even print(10). This is possible since a compiler gets to see the full code before turning it into machine instructions. For instance, it can release memory (or never allocate it) whenever a variable is no longer needed. Or it can transform the code entirely into an equivalent piece. To get a better idea, consider the following simulation of imperative programming (it is Python after all) below.英語のヒント
これにより、かなりの量の最適化ができる。第一に、多くの場合Pythonのインタープリタを飛ばせるので、CPU上の一つのPythonスレッドと組み合わさった複数の速いGPUで深刻になりうる性能のボトルネックを取り除ける。第二に、コンパイラは上のコードを最適化して、
print((1 + 2) + (3 + 4))や、さらにはprint(10)に書き直すかもしれない。コンパイラは、コードを機械語の命令に変える前にコード全体を見られるので、これが可能になる。たとえば、変数がもはや必要でなくなったらいつでもメモリを解放できる(あるいはそもそも割り当てない)。あるいは、コード全体を同等なものに変換できる。もっとよくわかるように、下の、命令型プログラミングのシミュレーション(結局のところPythonである)を考えよう。def add_():
return '''
def add(a, b):
return a + b
'''
def fancy_func_():
return '''
def fancy_func(a, b, c, d):
e = add(a, b)
f = add(c, d)
g = add(e, f)
return g
'''
def evoke_():
return add_() + fancy_func_() + 'print(fancy_func(1, 2, 3, 4))'
prog = evoke_()
print(prog)
y = compile(prog, '', 'exec')
exec(y)def add(a, b):
return a + b
def fancy_func(a, b, c, d):
e = add(a, b)
f = add(c, d)
g = add(e, f)
return g
print(fancy_func(1, 2, 3, 4))
10The differences between imperative (interpreted) programming and symbolic programming are as follows:
英語のヒント
命令型(インタープリタ型)プログラミングと記号的プログラミングの違いは次のとおりである。
- Imperative programming is easier. When imperative programming is used in Python, the majority of the code is straightforward and easy to write. It is also easier to debug imperative programming code. This is because it is easier to obtain and print all relevant intermediate variable values, or use Python’s built-in debugging tools.
英語のヒント
命令型プログラミングのほうが簡単である。Pythonで命令型プログラミングを使うと、コードの大部分は素直で書きやすい。命令型プログラミングのコードはデバッグも簡単である。関連する中間の変数の値をすべて取得して出力したり、Pythonの組み込みのデバッグ道具を使ったりするのが簡単だからである。 - Symbolic programming is more efficient and easier to port. Symbolic programming makes it easier to optimize the code during compilation, while also having the ability to port the program into a format independent of Python. This allows the program to be run in a non-Python environment, thus avoiding any potential performance issues related to the Python interpreter.
英語のヒント
記号的プログラミングのほうが効率的で、移植しやすい。記号的プログラミングでは、コンパイルのあいだにコードを最適化しやすく、またプログラムをPythonに依存しない形式に移植することもできる。これにより、プログラムをPython以外の環境で実行でき、Pythonのインタープリタにかかわる潜在的な性能の問題を避けられる。