Skip to content

Commit

Permalink
ci(dspy): Add Ruff linting workflow (stanfordnlp#538)
Browse files Browse the repository at this point in the history
* Add Ruff linting workflow

* Update GitHub Actions workflows

* Make ruff apply but not fail

* Style fixes by Ruff

* Combine Workflows

* Add caching

* Add import related rules

* Automatic Style fixes

* Try and fix caching

* Fix import

* Use natve python dep caching

* Update actions/checkout to v4

* Use autofix in tests

* Try to cache "install poetry"

* Empty-Commit to test caching

* Remove workflow deps

* Rename action

* Update workflow name

* Fix Docker stop command and subprocess run check

* Fix functional imports

---------

Co-authored-by: isaacbmiller <[email protected]>
  • Loading branch information
isaacbmiller and isaacbmiller authored Mar 4, 2024
1 parent 71ac993 commit a637bf4
Show file tree
Hide file tree
Showing 102 changed files with 428 additions and 293 deletions.
73 changes: 63 additions & 10 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and Run Tests
name: Fix, Test, and Build

on:
push:
Expand All @@ -10,38 +10,78 @@ env:
POETRY_VERSION: "1.6.1"

jobs:
fix:
name: Apply Ruff Fix
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v5
- uses: chartboost/ruff-action@v1
with:
args: --fix-only
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "Automatic Style fixes"

test:
name: Test
name: Run Tests
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9"]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Load cached Poetry installation
id: cached-poetry
uses: actions/cache@v3
with:
path: ~/.local
key: poetry-${{ env.POETRY_VERSION }}-${{ hashFiles('**/poetry.lock') }}
- name: Install Poetry
if: steps.cached-poetry.outputs.cache-hit != 'true'
uses: snok/install-poetry@v1
- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
cache: "poetry"
- name: Install dependencies
run: poetry install
run: poetry install --no-interaction --no-root
- name: Run lint with tests
uses: chartboost/ruff-action@v1
with:
args: --fix-only
- name: Run tests with pytest
run: poetry run pytest tests/

build_poetry:
name: Build Poetry
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9"]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Load cached Poetry installation
id: cached-poetry
uses: actions/cache@v3
with:
path: ~/.local
key: poetry-${{ env.POETRY_VERSION }}-${{ hashFiles('**/poetry.lock') }}
- name: Install Poetry
if: steps.cached-poetry.outputs.cache-hit != 'true'
uses: snok/install-poetry@v1
- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
cache: "poetry"
- name: Build
run: poetry build
- name: Install built package
Expand All @@ -50,17 +90,30 @@ jobs:
run: python -c "import dspy"
- name: Test import dsp
run: python -c "import dsp"

build_setup:
name: Build Setup
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9"]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Load cached Poetry installation
id: cached-poetry
uses: actions/cache@v3
with:
path: ~/.local
key: poetry-${{ env.POETRY_VERSION }}-${{ hashFiles('**/poetry.lock') }}
- name: Install Poetry
if: steps.cached-poetry.outputs.cache-hit != 'true'
uses: snok/install-poetry@v1
- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: "poetry"
- name: Run setup.py build
run: python setup.py build
1 change: 0 additions & 1 deletion dsp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from .templates import *
from .utils import settings


"""
TODO:
Expand Down
5 changes: 3 additions & 2 deletions dsp/evaluation/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import dsp
import tqdm
import pandas as pd
import tqdm

import dsp

try:
from IPython.display import display as ipython_display
Expand Down
26 changes: 11 additions & 15 deletions dsp/modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
from .cache_utils import *
from .azure_openai import AzureOpenAI
from .gpt3 import *
from .bedrock import *
from .cache_utils import *
from .clarifai import *
from .cohere import *
from .colbertv2 import ColBERTv2
from .databricks import *
from .google import *
from .gpt3 import *
from .hf import HFModel
from .colbertv2 import ColBERTv2
from .sentence_vectorizer import *
from .cohere import *
from .sbert import *
from .pyserini import *
from .hf_client import Anyscale, HFClientTGI, Together
from .ollama import *
from .clarifai import *
from .bedrock import *
from .google import *


from .hf_client import HFClientTGI
from .hf_client import Anyscale
from .hf_client import Together
from .pyserini import *
from .sbert import *
from .sentence_vectorizer import *
5 changes: 3 additions & 2 deletions dsp/modules/aws_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

from __future__ import annotations

from abc import abstractmethod
import json
import logging
from abc import abstractmethod
from typing import Any, Literal
import json

from dsp.modules.lm import LM

# Heuristic translating number of chars to tokens
Expand Down
3 changes: 2 additions & 1 deletion dsp/modules/azurecognitivesearch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Union, Any
from typing import Any, Union

from dsp.utils import dotdict

try:
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
Expand Down
3 changes: 2 additions & 1 deletion dsp/modules/bedrock.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

from typing import Any
import json
from typing import Any

from dsp.modules.aws_lm import AWSLM


Expand Down
5 changes: 2 additions & 3 deletions dsp/modules/cache_utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import os

from functools import wraps
from pathlib import Path

from joblib import Memory
from functools import wraps

from dsp.utils import dotdict


cache_turn_on = True


Expand Down
1 change: 1 addition & 0 deletions dsp/modules/clarifai.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from dsp.modules.lm import LM


class ClarifaiLLM(LM):
"""Integration to call models hosted in clarifai platform.
Expand Down
1 change: 1 addition & 0 deletions dsp/modules/cohere.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import math
from typing import Any, Optional

import backoff

from dsp.modules.lm import LM
Expand Down
4 changes: 2 additions & 2 deletions dsp/modules/colbertv2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import functools
from typing import Optional, Union, Any
from typing import Any, Optional, Union

import requests

from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory
from dsp.utils import dotdict


# TODO: Ideally, this takes the name of the index and looks up its port.


Expand Down
2 changes: 1 addition & 1 deletion dsp/modules/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from dsp.modules.gpt3 import GPT3

try:
from openai.openai_object import OpenAIObject
import openai.error
from openai.openai_object import OpenAIObject
ERRORS = (openai.error.RateLimitError, openai.error.ServiceUnavailableError, openai.error.APIError)
except Exception:
ERRORS = (openai.RateLimitError, openai.APIError)
Expand Down
20 changes: 11 additions & 9 deletions dsp/modules/finetuning/finetune_hf.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
# Adapted from: https://www.philschmid.de/fine-tune-flan-t5#3-fine-tune-and-evaluate-flan-t5

import os
import json
import copy
import glob
import torch
import json
import os
import warnings
from dataclasses import dataclass

import evaluate
import numpy as np
import torch
from datasets import Dataset
from dataclasses import dataclass
from transformers import (
set_seed,
AutoConfig,
AutoModelForSeq2SeqLM,
AutoModelForCausalLM,
AutoModelForSeq2SeqLM,
AutoTokenizer,
DataCollatorForSeq2Seq,
PreTrainedTokenizer,
Trainer,
Seq2SeqTrainer,
TrainingArguments,
Seq2SeqTrainingArguments,
DataCollatorForSeq2Seq,
Trainer,
TrainingArguments,
set_seed,
)

# from peft import get_peft_model, LoraConfig, TaskType
from transformers.trainer_callback import TrainerCallback

Expand Down
3 changes: 2 additions & 1 deletion dsp/modules/google.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from typing import Any, Optional
from collections.abc import Iterable
from typing import Any, Optional

import backoff

from dsp.modules.lm import LM
Expand Down
4 changes: 2 additions & 2 deletions dsp/modules/gpt3.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import json
from typing import Any, Literal, Optional, cast

import dsp
import backoff
import openai

import dsp
from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory, cache_turn_on
from dsp.modules.lm import LM

Expand All @@ -24,8 +24,8 @@
OPENAI_LEGACY = True

try:
from openai.openai_object import OpenAIObject
import openai.error
from openai.openai_object import OpenAIObject

ERRORS = (
openai.error.RateLimitError,
Expand Down
5 changes: 3 additions & 2 deletions dsp/modules/hf.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# from peft import PeftConfig, PeftModel
# from transformers import AutoModelForSeq2SeqLM, AutoModelForCausalLM, AutoTokenizer, AutoConfig
from typing import Optional, Literal
from typing import Literal, Optional

from dsp.modules.lm import LM

# from dsp.modules.finetuning.finetune_hf import preprocess_prompt

def openai_to_hf(**kwargs):
Expand Down Expand Up @@ -43,8 +44,8 @@ def __init__(self, model: str, checkpoint: Optional[str] = None, is_client: bool
self.device_map = hf_device_map
if not self.is_client:
try:
from transformers import AutoModelForSeq2SeqLM, AutoModelForCausalLM, AutoTokenizer, AutoConfig
import torch
from transformers import AutoConfig, AutoModelForCausalLM, AutoModelForSeq2SeqLM, AutoTokenizer
except ImportError as exc:
raise ModuleNotFoundError(
"You need to install Hugging Face transformers library to use HF models.",
Expand Down
15 changes: 7 additions & 8 deletions dsp/modules/hf_client.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import os
import random
import requests
from dsp.modules.hf import HFModel, openai_to_hf
from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory
import subprocess
import re
import shutil
import subprocess

# from dsp.modules.adapter import TurboAdapter, DavinciAdapter, LlamaAdapter

import backoff
import requests

from dsp.modules.cache_utils import CacheMemory, NotebookCacheMemory
from dsp.modules.hf import HFModel, openai_to_hf

ERRORS = (Exception)

Expand Down Expand Up @@ -173,7 +173,7 @@ def close_server(self, port):
container_id = match.group(1)
port_mapping = subprocess.check_output(['docker', 'port', container_id]).decode().strip()
if f'0.0.0.0:{port}' in port_mapping:
subprocess.run(['docker', 'stop', container_id])
subprocess.run(['docker', 'stop', container_id], check=False)

def run_server(self, port, model_name=None, model_path=None, env_variable=None, gpus="all", num_shard=1, max_input_length=4000, max_total_tokens=4096, max_best_of=100):
self.close_server(port)
Expand Down Expand Up @@ -355,8 +355,7 @@ class ChatModuleClient(HFModel):
def __init__(self, model, model_path):
super().__init__(model=model, is_client=True)

from mlc_chat import ChatModule
from mlc_chat import ChatConfig
from mlc_chat import ChatConfig, ChatModule

self.cm = ChatModule(
model=model, lib_path=model_path, chat_config=ChatConfig(conv_template="LM"),
Expand Down
Loading

0 comments on commit a637bf4

Please sign in to comment.