Developer Start Guide

Contributing

You want to contribute to Theano? That is great! This page explain ourworkflow and some resource for doing so.

Looking for an idea for a first contribution? Check the github issueswith a label easy fix. They are good starter. It is recommandedthat you write on the issue you want to work on it. This help makesure it is up to date and see if nobody else is working on it. Also,we can sometimes provides more information about it. There is alsothe label NeedSomeoneToFinish that isinterresting to check. The difficulty level is variable.

Resources

See Community for a list of Theano resources. Thefollowing groups/mailing-lists are especially useful to Theanocontributors: theano-dev, theano-buildbot, and theano-github.

To get up to speed, you’ll need to

Requirements for Quality Contributions

  • All the code should be properly tested.
  • The code should be compatible with Python 2.7 and above, as well as Python3.3 and above (using six if needed).
  • All the code should respect thePEP8 Code Style Guide.
  • The docstrings of all the classes and functions should respect thePEP257 rules and follow theNumpy docstring standard.

Each point will be referred to more in detail in the following.

Unit tests

When you submit a pull request, your changes will automatically betested via Travis-CI. This will post the results of the tests with alittle icon next to your commit. A yellow circle means the tests arerunning. A red X means the tests failed and a green circle means thetests passed.

Just because the tests run automatically does not mean you shouldn’trun them yourself to make sure everything is all right. You can runonly the portion you are modifying to go faster and have travis tomake sure there are no global impacts.

Also, if you are changing GPU code, travis doesn’t test that, becausethere are no GPUs on the test nodes.

To run the test suite with the default options, seeHow to test that Theano works properly.

Each night we execute all the unit tests automatically, with severalsets of options. The result is sent by email to the theano-buildbotmailing list.

For more detail, see The nightly build/tests process.

To run all the tests with the same configuration as the buildbot, runthis script:

  1. theano/misc/do_nightly_build

This script accepts arguments that it forwards to nosetests. You canrun only some tests or enable pdb by giving the equivalent nosetestsparameters.

Setting up your Editor for PEP8

Here are instructions for Vim and Emacs. If you have similar instructions for other text editorsor IDE, please let us know and we will update this documentation.

Vim

Detection of warnings and errors is done by the pep8 script(or flake8, that also checks for other things, like syntaxerrors). Syntax highlighting and general integration into Vim is done bythe Syntastic plugin for Vim.

To setup VIM:

  • Install flake8 (if not already installed) with:
  1. pip install "flake8<3"

Warning

Starting version 3.0.0, flake8 changed its dependancies andmoved its Python API to a legacy module, breaking Theano’s flake8 tests.We recommend using a version prior to 3.

Note

You can use easy_install instead of pip, and pep8instead of flake8 if you prefer. The important thing is that theflake8 or pep8 executable ends up in your $PATH.

  • Install vundle with:
  1. git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  • Edit ~/.vimrc and add the lines:
  1. set nocompatible " be iMproved, required
  2. filetype off " required
  3. " set the runtime path to include Vundle and initialize
  4. set rtp+=~/.vim/bundle/Vundle.vim
  5. call vundle#begin()
  6.  
  7. Plugin 'gmarik/Vundle.vim' " let Vundle manage Vundle (required!)
  8. Plugin 'scrooloose/syntastic'
  9. Plugin 'jimf/vim-pep8-text-width'
  10.  
  11. call vundle#end()
  12.  
  13. " Syntastic settings
  14. " You can run checkers explicitly by calling :SyntasticCheck <checker
  15. let g:syntastic_python_checkers = ['flake8'] "use one of the following checkers:
  16. " flake8, pyflakes, pylint, python (native checker)
  17. let g:syntastic_enable_highlighting = 1 "highlight errors and warnings
  18. let g:syntastic_style_error_symbol = ">>" "error symbol
  19. let g:syntastic_warning_symbol = ">>" "warning symbol
  20. let g:syntastic_check_on_open = 1
  21. let g:syntastic_auto_jump = 0 "do not jump to errors when detected
  • Open a new vim and run :PluginInstall to automatically install theplugins. When the installation is done, close the installation “window”with :q.From now on Vim will check for PEP8 errors and highlight them whenever afile is saved.
A few useful commands
  • Open the list of errors: :lopen, that can be abbreviated in :lop(denoted :lop[en]).
  • Close that list: :lcl[ose].
  • Next error: :lne[xt].
  • Previous error: :lp[revious].

Once you fix errors, messages and highlighting will still appear in thefixed file until you save it again.

We can also configure the ~/.vimrc to make it easier to work with Syntastic.For instance, to add a summary in the status bar, you can add:

  1. set statusline+=%{SyntasticStatuslineFlag()}

To bind F2 and F3 to navigate to previous and next error, you can add:

  1. map <F2> :lprevious<CR>
  2. map <F3> :lnext<CR>

You can prefix those by autocmd FileType python if you want thesebindings to work only on Python files.

Emacs

There is an excellent system to configure emacs for Python:emacs-for-python. It gathers manyemacs config into one, and modifies them to behave together nicely. Youcan use it to check for pep8 compliance and for Python syntax errors.

To install it on Linux, you can do like this:

  1. cd
  2. git clone https://github.com/gabrielelanaro/emacs-for-python.git ~/.emacs.d/emacs-for-python

Then in your ~/.emacs file, add this:

  1. ;; Mandatory
  2. (load-file "~/.emacs.d/emacs-for-python/epy-init.el")
  3. (add-to-list 'load-path "~/.emacs.d/emacs-for-python/") ;; tell where to load the various files
  4.  
  5. ;; Each of them enables different parts of the system.
  6. ;; Only the first two are needed for pep8, syntax check.
  7. (require 'epy-setup) ;; It will setup other loads, it is required!
  8. (require 'epy-python) ;; If you want the python facilities [optional]
  9. (require 'epy-completion) ;; If you want the autocompletion settings [optional]
  10. (require 'epy-editing) ;; For configurations related to editing [optional]
  11. ;; [newer version of emacs-for-python]
  12. (require 'epy-nose) ;; For shortcut to call nosetests [optional]
  13.  
  14. ;; Define f10 to previous error
  15. ;; Define f11 to next error
  16. (require 'epy-bindings) ;; For my suggested keybindings [optional]
  17.  
  18. ;; Some shortcut that do not collide with gnome-terminal,
  19. ;; otherwise, "epy-bindings" define f10 and f11 for them.
  20. (global-set-key [f2] 'flymake-goto-prev-error)
  21. (global-set-key [f3] 'flymake-goto-next-error)
  22.  
  23. ;; Next two lines are the checks to do. You can add more if you wish.
  24. (epy-setup-checker "pyflakes %f") ;; For python syntax check
  25. (epy-setup-checker "pep8 -r %f") ;; For pep8 check

Note

The script highlights problematic lines. This can make part of theline not readable depending on the background. To replace the linehighlight by an underline, add this to your emacs configurationfile:

;; Make lines readable when there is an warning optional) (:underline “red”))))‘(flymake-warnline ((((class color)) (:underline “yellow”)))))

Documentation and docstrings

A Docstring Example

Here is an example on how to add a docstring to a class.

  1. import theano
  2.  
  3. class DoubleOp(theano.Op):
  4. """
  5. Double each element of a tensor.
  6.  
  7. Parameters
  8. ----------
  9. x : tensor
  10. Input tensor
  11.  
  12. Returns
  13. -------
  14. tensor
  15. a tensor of the same shape and dtype as the input with all
  16. values doubled.
  17.  
  18. Notes
  19. -----
  20. this is a test note
  21.  
  22. See Also
  23. --------
  24. :class:`~theano.tensor.elemwise.Elemwise` : You can use this to replace
  25. this example. Just execute `x * 2` with x being a Theano variable.
  26.  
  27.  
  28. .. versionadded:: 0.6
  29. """

This is how it will show up for files that we auto-list in the librarydocumentation:

  • class theano.misc.doubleop.DoubleOp[source]
  • Double each element of a tensor.

Parameters:x (tensor) – Input tensorReturns:a tensor of the same shape and dtype as the input with allvalues doubled.Return type:tensor

Notes

this is a test note

See also

Elemwise : You can use this to replacethis example. Just execute x * 2 with x being a Theano variable.

New in version 0.6.

Installation and configuration

To obtain developer access: register with GitHub and create a fork of Theano.

This will create your own Theano project on GitHub, referred lateras “YourProfile/Theano”, or “origin”, from which you will be able tocontribute to the original Theano/Theano, also called “central”.

Create a local copy

Clone your fork locally with

  1. git clone git@github.com:YOUR_GITHUB_LOGIN/Theano.git

For this URL to work, you must set your public ssh keys inside yourgithub account setting.

From your local repository, your own fork on GitHub will be called “origin”.

Then, add a reference to the original (“central”) Theano repository with

  1. git remote add central git://github.com/Theano/Theano.git

You can choose another name than “central” to reference Theano/Theano(for instance, NumPy uses “upstream”), but this documentation will stickto “central.”

You can then test your installation of Theano by following the steps ofHow to test that Theano works properly.

Using your local copy

To update your library to the latest revision, you should have a local branchthat tracks central/master. You can add one (named “trunk” here) with:

  1. git fetch central
  2. git branch trunk central/master

Once you have such a branch, in order to update it, do:

  1. git checkout trunk
  2. git pull

Keep in mind that this branch should be “read-only”: if you want topatch Theano, you should work in another branch, like described in theDevelopment Workflow section below.

Configure Git

On your local machine, you need to configure git with basic informations:

  1. git config --global user.email you@yourdomain.example.com
  2. git config --global user.name "Your Name Comes Here"

You can also instruct git to use color in diff. For this, you need toadd those lines in the file ~/.gitconfig

  1. [color]
  2. branch = auto
  3. diff = auto
  4. interactive = auto
  5. status = auto

Development Workflow

Start a new local branch

When working on a new feature in your own fork, start from an up-to-date copyof the master branch (the principal one) of the central repository(Theano/Theano on GitHub):

  1. git fetch central
  2. git checkout -b my_shiny_feature central/master

Note

This last line is a shortcut for:

  1. git branch my_shiny_feature central/master
  2. git checkout my_shiny_feature

Submit your changes to the central repository

Once your code is ready for others to review, you need to commit all the changes and then push yourbranch to your github fork first:

  1. git commit -a -m "your message here"
  1. git push -u origin my_shiny_feature

Then, go to your fork’s github page on the github website, select yourfeature branch and hit the “Pull Request” button in the top rightcorner. This will signal the maintainers that you wish to submit yourchanges for inclusion in central/master.If you don’t get any feedback, bug us on the theano-dev mailing list.

Address reviewer comments

Your pull request will be reviewed by members of the core developmentteam. If your branch is not directly accepted, the reviewers will useGitHub’s system to add “notes”, either general (on the entire commit),or “line notes”, relative to a particular line of code.In order to have the pull request accepted, you may have to answerthe reviewer’s questions, you can do that on GitHub.

You may also have to edit your code to address their concerns. Someof the usual requests include fixing typos in comments, adding orcorrecting comments, adding unit tests in the test suite. In order todo that, you should continue your edits in the same branch you used (inthis example, “my_shiny_feature”). For instance, if you changed yourworking branch, you should first:

  1. git checkout my_shiny_feature

Then, edit your code, and test it appropriately (seeRequirements for Quality Contributions below), and push it again to your GitHubfork, like the first time (except the -u option is only needed thefirst time):

  1. git push origin my_shiny_feature

The pull request to the central repository will then be automaticallyupdated by GitHub. However, the reviewers will not be automaticallynotified of your revision, so it is advised to reply to the comments onGitHub, to let them know that you have submitted a fix.

More Advanced Git Usage

You can find information and tips in the numpy developmentpage. Here are a few.

Cleaning up branches

When your pull request has been merged, you can delete the branch fromyour GitHub fork’s list of branches. This is useful to avoid having toomany branches staying there. Deleting this remote branch is achievedwith:

  1. git push origin :my_shiny_feature

This lines pushes to the “origin” repository (your fork of Theano onGitHub), into the branch “my_shiny_feature”, an empty content (that’swhy there is nothing before the colon), effectively removing it.

The branch will still be present in your local clone of the repository.If you want to delete it from there, too, you can run:

  1. git branch -d my_shiny_feature

Amending a submitted pull request

If you want to fix a commit already submitted within a pull request(e.g. to fix a small typo), before the pull request is accepted, you cando it like this to keep history clean:

  1. git checkout my_shiny_feature
  2. git commit --amend
  3. git push origin my_shiny_feature:my_shiny_feature

Do not abuse that command, and please use it only when there are onlysmall issues to be taken care of. Otherwise, it becomes difficult tomatch the comments made by reviewers with the new modifications.In the general case, you should stick with the approach described above.

Cleaning up history

Sometimes you may have commits in your feature branch thatare not needed in the final pull request. There is a page that talks aboutthis. In summary:

  • Commits to the trunk should be a lot cleaner than commits to yourfeature branch; not just for ease of reviewing but alsobecause intermediate commits can break blame (the bisecting tool).
  • git merge –squash will put all of the commits from your feature branch into one commit.
  • There are other tools that are useful if your branch is too big for one squash.

Add another distant repository

To collaborate with another user on some feature he is developing, andthat is not ready for inclusion in central, the easiest way is to use abranch of their Theano fork (usually on GitHub).

Just like we added Theano/Theano as a remote repository, named“central”, you can add (on your local machine) a reference to their forkas a new remote repository. REPO_NAME is the name you choose to namethis fork, and GIT_REPO_PATH is the URL of the fork in question.

  1. git remote add REPO_NAME GIT_REPO_PATH

Then, you can create a new local branch (LOCAL_BRANCH_NAME) based ona specific branch (REMOTE_BRANCH_NAME) from the remote repository(REPO_NAME):

  1. git checkout -b LOCAL_BRANCH_NAME REPO_NAME/REMOTE_BRANCH_NAME

Other tools that can help you

  • cProfile: time profiler that work at function level.
  • Yep: A module for profiling compiled extensions.
  • autopep8: A tool that automatically formats Python code to conform to the PEP 8 style guide.
  • line_profiler: Line-by-line profiler.
  • memory_profiler: memory profiler
  • runsnake: Gui for cProfile(time profiler) and Meliae(memory profiler)
  • Guppy: Supports object and heap memory sizing, profiling and debugging.
  • hub: A tool that adds github commands to the git command line.
  • git pull-requests: Another tool for git/github command line.