Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Same as ContextualVersionConflict exception in Google Colab issue #108 #112

Closed
chrstnsusai opened this issue Feb 4, 2021 · 8 comments
Closed

Comments

@chrstnsusai
Copy link

chrstnsusai commented Feb 4, 2021

  • CodeCarbon version:1.0.0
  • Python version: 3.6.9
  • Operating System: Ubuntu 18.04.5 LTS (Bionic Beaver)

Description

Describe what you were trying to get done. Your example
Tell us what happened, what went wrong, and what you expected to happen.

Example works fine before 2021/01/15 never after

What I Did

Your example

!pip install codecarbon

import tensorflow as tf

from codecarbon import EmissionsTracker

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0


model = tf.keras.models.Sequential(
    [
        tf.keras.layers.Flatten(input_shape=(28, 28)),
        tf.keras.layers.Dense(128, activation="relu"),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Dense(10),
    ]
)

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

model.compile(optimizer="adam", loss=loss_fn, metrics=["accuracy"])

tracker = EmissionsTracker()
tracker.start()
model.fit(x_train, y_train, epochs=10)
emissions: float = tracker.stop()
print(emissions)_

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.

Collecting codecarbon
  Downloading https://files.pythonhosted.org/packages/1d/78/0f6f06250558ca5dc21abb28660fecb78f09b4d5258d8e634393a780d1a6/codecarbon-1.0.0-py3-none-any.whl (49kB)
     |████████████████████████████████| 51kB 2.8MB/s 
Collecting APScheduler
  Downloading https://files.pythonhosted.org/packages/5d/e9/b5e4f7aaea076bbfb241b04824c63cd42fe8029358103c27375e690bddc0/APScheduler-3.7.0-py2.py3-none-any.whl (59kB)
     |████████████████████████████████| 61kB 4.1MB/s 
Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from codecarbon) (2.23.0)
Collecting dash
  Downloading https://files.pythonhosted.org/packages/bc/b4/0bd5c94fdcb0eccb93c3c8068fe10f5607e542337d0b8f6e2d88078316a9/dash-1.19.0.tar.gz (75kB)
     |████████████████████████████████| 81kB 3.9MB/s 
Collecting fire
  Downloading https://files.pythonhosted.org/packages/11/07/a119a1aa04d37bc819940d95ed7e135a7dcca1c098123a3764a6dcace9e7/fire-0.4.0.tar.gz (87kB)
     |████████████████████████████████| 92kB 4.7MB/s 
Requirement already satisfied: dataclasses in /usr/local/lib/python3.6/dist-packages (from codecarbon) (0.8)
Requirement already satisfied: pandas in /usr/local/lib/python3.6/dist-packages (from codecarbon) (1.1.5)
Collecting dash-bootstrap-components
  Downloading https://files.pythonhosted.org/packages/1d/26/17f8b18a79bae9e278b7674ad0aef8007e87b4d2280525a95103fd2a8fe5/dash_bootstrap_components-0.11.1-py2.py3-none-any.whl (187kB)
     |████████████████████████████████| 194kB 6.4MB/s 
Collecting pynvml
  Downloading https://files.pythonhosted.org/packages/1b/1a/a25c143e1d2f873d67edf534b269d028dd3c20be69737cca56bf28911d02/pynvml-8.0.4-py3-none-any.whl
Requirement already satisfied: six>=1.4.0 in /usr/local/lib/python3.6/dist-packages (from APScheduler->codecarbon) (1.15.0)
Requirement already satisfied: pytz in /usr/local/lib/python3.6/dist-packages (from APScheduler->codecarbon) (2018.9)
Collecting tzlocal~=2.0
  Downloading https://files.pythonhosted.org/packages/5d/94/d47b0fd5988e6b7059de05720a646a2930920fff247a826f61674d436ba4/tzlocal-2.1-py2.py3-none-any.whl
Requirement already satisfied: setuptools>=0.7 in /usr/local/lib/python3.6/dist-packages (from APScheduler->codecarbon) (53.0.0)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->codecarbon) (1.24.3)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->codecarbon) (2.10)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->codecarbon) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->codecarbon) (2020.12.5)
Requirement already satisfied: Flask>=1.0.4 in /usr/local/lib/python3.6/dist-packages (from dash->codecarbon) (1.1.2)
Collecting flask-compress
  Downloading https://files.pythonhosted.org/packages/b2/7a/9c4641f975fb9daaf945dc39da6a52fd5693ab3bbc2d53780eab3b5106f4/Flask_Compress-1.8.0-py3-none-any.whl
Requirement already satisfied: plotly in /usr/local/lib/python3.6/dist-packages (from dash->codecarbon) (4.4.1)
Collecting dash_renderer==1.9.0
  Downloading https://files.pythonhosted.org/packages/be/a6/dd1edfe7b1102274e93991736c35b2a5e1a63b524c8d9f41bbb30f17340b/dash_renderer-1.9.0.tar.gz (1.0MB)
     |████████████████████████████████| 1.0MB 7.2MB/s 
Collecting dash-core-components==1.15.0
  Downloading https://files.pythonhosted.org/packages/22/78/ae0829e673f3df77403bcdb35073b1ed2f156080f5bcac6f21c1047d73fe/dash_core_components-1.15.0.tar.gz (3.5MB)
     |████████████████████████████████| 3.5MB 13.2MB/s 
Collecting dash-html-components==1.1.2
  Downloading https://files.pythonhosted.org/packages/3f/25/56de2708128fe375eecc2e18e0ccdc3a853494966e36334ec8a30be99b94/dash_html_components-1.1.2.tar.gz (188kB)
     |████████████████████████████████| 194kB 40.6MB/s 
Collecting dash-table==4.11.2
  Downloading https://files.pythonhosted.org/packages/3d/ae/96cb64b58d76391604b57f8c747f9a19ab2122e7ba214e2e0cf35484962b/dash_table-4.11.2.tar.gz (1.8MB)
     |████████████████████████████████| 1.8MB 38.3MB/s 
Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from dash->codecarbon) (0.16.0)
Requirement already satisfied: termcolor in /usr/local/lib/python3.6/dist-packages (from fire->codecarbon) (1.1.0)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.6/dist-packages (from pandas->codecarbon) (2.8.1)
Requirement already satisfied: numpy>=1.15.4 in /usr/local/lib/python3.6/dist-packages (from pandas->codecarbon) (1.19.5)
Requirement already satisfied: itsdangerous>=0.24 in /usr/local/lib/python3.6/dist-packages (from Flask>=1.0.4->dash->codecarbon) (1.1.0)
Requirement already satisfied: click>=5.1 in /usr/local/lib/python3.6/dist-packages (from Flask>=1.0.4->dash->codecarbon) (7.1.2)
Requirement already satisfied: Jinja2>=2.10.1 in /usr/local/lib/python3.6/dist-packages (from Flask>=1.0.4->dash->codecarbon) (2.11.2)
Requirement already satisfied: Werkzeug>=0.15 in /usr/local/lib/python3.6/dist-packages (from Flask>=1.0.4->dash->codecarbon) (1.0.1)
Collecting brotli
  Downloading https://files.pythonhosted.org/packages/b4/d3/7c98f05b7b9103e2f3a112ba42f269c798155b3e5404fb80bb8f823aaebe/Brotli-1.0.9-cp36-cp36m-manylinux1_x86_64.whl (357kB)
     |████████████████████████████████| 358kB 41.2MB/s 
Requirement already satisfied: retrying>=1.3.3 in /usr/local/lib/python3.6/dist-packages (from plotly->dash->codecarbon) (1.3.3)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.6/dist-packages (from Jinja2>=2.10.1->Flask>=1.0.4->dash->codecarbon) (1.1.1)
Building wheels for collected packages: dash, fire, dash-renderer, dash-core-components, dash-html-components, dash-table
  Building wheel for dash (setup.py) ... done
  Created wheel for dash: filename=dash-1.19.0-cp36-none-any.whl size=84013 sha256=08dc3f0960a54ea6da81e46a8b2a86f2edbe642fc90919781281d6a8886e4dee
  Stored in directory: /root/.cache/pip/wheels/f7/50/a7/a230ff7f503b10120bff18c2524a375bb85a61ce6b519c8a77
  Building wheel for fire (setup.py) ... done
  Created wheel for fire: filename=fire-0.4.0-py2.py3-none-any.whl size=115928 sha256=98b65b034d06b4f56aa4bb96d6f370f73979c28c85c859735497f9730ea62c04
  Stored in directory: /root/.cache/pip/wheels/af/19/30/1ea0cad502dcb4e66ed5a690279628c827aea38bbbab75d5ed
  Building wheel for dash-renderer (setup.py) ... done
  Created wheel for dash-renderer: filename=dash_renderer-1.9.0-cp36-none-any.whl size=1014869 sha256=50e43efd2f7bce53545b685a442ae465944268e14b39046367d6bf9065392c26
  Stored in directory: /root/.cache/pip/wheels/46/a0/ec/2be2e8fc750e623d76f9690c397cc5ab28b33d0a16a49e10c5
  Building wheel for dash-core-components (setup.py) ... done
  Created wheel for dash-core-components: filename=dash_core_components-1.15.0-cp36-none-any.whl size=3527014 sha256=3aa1d57e14283ec396209981d532ce23ccc4797878ad6bccbbc0382cdfe67191
  Stored in directory: /root/.cache/pip/wheels/53/3d/be/d628d6f66eedf9597f0c89c8ff43a5020ad1c25152c77d8e9f
  Building wheel for dash-html-components (setup.py) ... done
  Created wheel for dash-html-components: filename=dash_html_components-1.1.2-cp36-none-any.whl size=427830 sha256=f8dd0350e4bbd15712dc7b37d39081308888de23f12117990ed289595fc9be13
  Stored in directory: /root/.cache/pip/wheels/44/95/70/0dc41f9b4e31b8a7ea22193aad5647b2c85cfab37bf13c0242
  Building wheel for dash-table (setup.py) ... done
  Created wheel for dash-table: filename=dash_table-4.11.2-cp36-none-any.whl size=1839870 sha256=d164847d466bc4fc01a6162506ce077e1e4ee4f8831b83b176c153e5d351f9fe
  Stored in directory: /root/.cache/pip/wheels/72/d9/f6/2ad62ac0037f1f0c0d3d10948a596b594a069057df0656ac3f
Successfully built dash fire dash-renderer dash-core-components dash-html-components dash-table
Installing collected packages: tzlocal, APScheduler, brotli, flask-compress, dash-renderer, dash-core-components, dash-html-components, dash-table, dash, fire, dash-bootstrap-components, pynvml, codecarbon
  Found existing installation: tzlocal 1.5.1
    Uninstalling tzlocal-1.5.1:
      Successfully uninstalled tzlocal-1.5.1
Successfully installed APScheduler-3.7.0 brotli-1.0.9 codecarbon-1.0.0 dash-1.19.0 dash-bootstrap-components-0.11.1 dash-core-components-1.15.0 dash-html-components-1.1.2 dash-renderer-1.9.0 dash-table-4.11.2 fire-0.4.0 flask-compress-1.8.0 pynvml-8.0.4 tzlocal-2.1
---------------------------------------------------------------------------
ContextualVersionConflict                 Traceback (most recent call last)
<ipython-input-2-43bdafe14602> in <module>()
      3 import tensorflow as tf
      4 
----> 5 from codecarbon import EmissionsTracker
      6 
      7 mnist = tf.keras.datasets.mnist

6 frames
/usr/local/lib/python3.6/dist-packages/codecarbon/__init__.py in <module>()
      3 """
      4 
----> 5 from .emissions_tracker import (
      6     EmissionsTracker,
      7     OfflineEmissionsTracker,

/usr/local/lib/python3.6/dist-packages/codecarbon/emissions_tracker.py in <module>()
     13 from typing import Callable, List, Optional
     14 
---> 15 from apscheduler.schedulers.background import BackgroundScheduler
     16 
     17 from codecarbon.core import cpu, gpu

/usr/local/lib/python3.6/dist-packages/apscheduler/__init__.py in <module>()
      2 
      3 try:
----> 4     release = get_distribution('APScheduler').version.split('-')[0]
      5 except DistributionNotFound:
      6     release = '3.5.0'

/usr/local/lib/python3.6/dist-packages/pkg_resources/__init__.py in get_distribution(dist)
    464         dist = Requirement.parse(dist)
    465     if isinstance(dist, Requirement):
--> 466         dist = get_provider(dist)
    467     if not isinstance(dist, Distribution):
    468         raise TypeError("Expected string, Requirement, or Distribution", dist)

/usr/local/lib/python3.6/dist-packages/pkg_resources/__init__.py in get_provider(moduleOrReq)
    340     """Return an IResourceProvider for the named module or requirement"""
    341     if isinstance(moduleOrReq, Requirement):
--> 342         return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
    343     try:
    344         module = sys.modules[moduleOrReq]

/usr/local/lib/python3.6/dist-packages/pkg_resources/__init__.py in require(self, *requirements)
    884         included, even if they were already activated in this working set.
    885         """
--> 886         needed = self.resolve(parse_requirements(requirements))
    887 
    888         for dist in needed:

/usr/local/lib/python3.6/dist-packages/pkg_resources/__init__.py in resolve(self, requirements, env, installer, replace_conflicting, extras)
    775                 # Oops, the "best" so far conflicts with a dependency
    776                 dependent_req = required_by[req]
--> 777                 raise VersionConflict(dist, req).with_context(dependent_req)
    778 
    779             # push the new requirements onto the stack

ContextualVersionConflict: (tzlocal 1.5.1 (/usr/local/lib/python3.6/dist-packages), Requirement.parse('tzlocal~=2.0'), {'APScheduler'})
@Lothiraldan
Copy link
Contributor

If you ever get a ContextualVersionConflict on jupyter notebook or Google Colab, try restarting the Runtime/Kernel. The error means that a new version has been installed of a Python package that was already loaded.

@vict0rsch
Copy link
Contributor

@Lothiraldan closed?

@grimelda
Copy link

grimelda commented Jul 6, 2021

Sadly, this error seems to persist... try any new colab notebook with the following code (reproducible), it will give this same error.

!pip install codecarbon
from codecarbon import track_emissions

Similarly, in existing colab notebooks when they are started on a new instance (on open or when Runtime > Factory restart) same ContextualVersionConflict error will pop up every time. The following: !pip install --upgrade tzlocal==2.0 or !pip install --upgrade APScheduler do nothing to solve the issue.

My current workaround after this error is to restart kernel after refreshing instance (Runtime > Factory restart & Runtime > Restart and run all) which seems messy.

In any case, the error also pops up when `from codecarbon import EmissionsTracker' is used.

@grimelda
Copy link

@Lothiraldan in my experience this issue still persists.. Can i contribute anything to solving this?

@Lothiraldan
Copy link
Contributor

@grimelda After installing packages on Google Colab (I recommend using %pip install -U codecarbon to do so), you need to restart the Runtime. Google Colab seems to start from a non-empty state, some packages are pre-installed, you can see which ones with %pip list. From this list, it looks like tzlocal is preinstall in version 1.5.1. While installing codecarbon, we install apscheduler which requires a higher version so the version tzlocal-2.1 is installed instead. Then when loading apscheduler, an automatic check happens on the dependencies:

/usr/local/lib/python3.6/dist-packages/pkg_resources/__init__.py in require(self, *requirements)
    884         included, even if they were already activated in this working set.
    885         """
--> 886         needed = self.resolve(parse_requirements(requirements))

And it looks like that the old version 1.5.1 is still in memory somewhere. After restarting the Runtime, the version on disk is 2.1 so everything works as expected.

@grimelda
Copy link

grimelda commented Jul 19, 2021

I'm trying to get my team to log the use of intensive scripts, and the decorator seems like a great and easy to implement option that i can ask my team to adopt, however I can hardly ask them to restart their kernel every time a script is loaded. A workaround involving I could instruct to os._exit(0) to force a kernel restart but this would be extremely ugly.
This is the kind of workflow that I would expect to work, but doesn't in Colab:

!pip install -U codecarbon
from codecarbon import track_emissions

@track_emissions(project_name=f"projectname")
def main():
    print('Something')

Additional attempts to run codecarbon in colab without triggering this warning on first run that have failed:

# to get rid of tzlocal==1.5.1  in memory does not work, prompts y/n 
!pip uninstall tzlocal
!pip install -U codecarbon
from codecarbon import track_emissions
# to force reload (and purge previous versions using importlib) does not work
!pip install -U codecarbon
import importlib, tzlocal, pytz
importlib.reload(pytz)
importlib.reload(tzlocal)
from codecarbon import track_emissions
# to get rid of old version tzlocal==1.5.1 initialised by colab does not work
!pip uninstall tzlocal
!pip install -U codecarbon
from codecarbon import track_emissions
# force reinstall of tzlocal==2.0 does not work, triggers warning also:
# WARNING: The following packages were previously imported in this runtime: [pytz] You must restart the runtime in order to use newly installed versions.

!pip install --force-reinstall tzlocal==2.0
!pip install -U codecarbon
from codecarbon import track_emissions

@vict0rsch
Copy link
Contributor

Would this make the kernel re-read files and update the in-memory version of tzlocal?

from importlib import reload  
import tzlocal
---
!pip uninstall tzlocal
!pip install --force-reinstall tzlocal==2.0
---
reload(tzlocal)

@grimelda
Copy link

grimelda commented Jul 20, 2021

Tried something similar which didn't work, and updated it to vict0rsch' method, sadly does not work. I saved a bunch of attempts in the gist below for documentation.

My main finding is that the error pops up earlier by the importing of pytz which is a dependency of tzlocal and APScheduler. The error in fist instance is silent, and is only raised when codecarbon is imported. This goes over my head wrt how to solve this. Is this a colab issue?

WARNING: The following packages were previously imported in this runtime:
  [pytz]
You must restart the runtime in order to use newly installed versions.
\nContextualVersionConflict: (tzlocal 1.5.1 (/usr/local/lib/python3.7/dist-packages), Requirement.parse('tzlocal~=2.0'), {'APScheduler'})\n

https://gist.github.com/grimelda/2f1d1e382b0b3984e95f9f2582848613

edit: included workaround using kernel restart prompt in gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants