python_code
stringlengths
0
4.04M
repo_name
stringlengths
8
58
file_path
stringlengths
5
147
# Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. from copy import deepcopy import numpy as np import torch from botorch.utils.sampling import draw_sobol_samples import sys sys.path.append('..') from plot_config import * from problems import DiscrimLowDim from aepsych.models.gp_classification import GPClassificationModel from aepsych.factory.factory import default_mean_covar_factory from aepsych.config import Config from aepsych.acquisition.lookahead_utils import lookahead_at_xstar def make_figure(): # Generate training data for the model prob = DiscrimLowDim() X = draw_sobol_samples( bounds=torch.tensor(prob.bounds, dtype=torch.double), n=20, q=1, seed=1403 ).squeeze(1) np.random.seed(1403) y = torch.LongTensor([1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1]) ###print(X) ###tensor([[ 0.2829, 0.2585], ###[-0.8400, -0.4620], ###[-0.3771, 0.8218], ###[ 0.9996, -0.8986], ###[ 0.5347, 0.6318], ###[-0.0918, -0.5853], ###[-0.6252, 0.1951], ###[ 0.2478, -0.0219], ###[ 0.0526, 0.9270], ###[-0.5706, -0.8485], ###[-0.1469, 0.4888], ###[ 0.7304, -0.2870], ###[ 0.8047, 0.0576], ###[-0.3227, -0.2292], ###[-0.8948, 0.6194], ###[ 0.4783, -0.6676], ###[ 0.3968, 0.5543], ###[-0.9803, -0.7246], ###[-0.3026, 0.1158], ###[ 0.8207, -0.1633]], dtype=torch.float64) # Fit a model lb, ub = prob.bounds config = deepcopy(mean_covar_config) config["lb"] = str(lb.tolist()) config["ub"] = str(ub.tolist()) mean, covar = default_mean_covar_factory(Config({"default_mean_covar_factory": config})) m = GPClassificationModel(lb=lb, ub=ub, mean_module=mean, covar_module=covar) m.fit(train_x=X, train_y=y) # Create a grid for plotting ngrid = 25 xp = np.linspace(-1, 1, ngrid) yp = np.linspace(-1, 1, ngrid) xv, yv = np.meshgrid(xp, yp) x_plt = torch.tensor(np.vstack((xv.flatten(), yv.flatten())).T) indx_star = 165 Xstar = x_plt[indx_star, :].unsqueeze(0) Xq = x_plt Px, P1, P0, py1 = lookahead_at_xstar(model=m, Xstar=Xstar, Xq=x_plt, gamma=prob.f_threshold) fig = plt.figure(figsize=(6.75, 1.75)) # Plot the not-look-ahead level-set posterior axs = [] ax = fig.add_subplot(131) axs.append(ax) ax.contourf(yv, xv, Px.detach().numpy().reshape(ngrid, ngrid), levels=np.linspace(0, 1, 20), alpha=0.2) ax.set_title('Level-set posterior\n$\pi(\mathbf{x} | \mathcal{D}_n)$') y_is_1 = y == 1 ax.plot(X[y_is_1, 1], X[y_is_1, 0], 'kx', ls='None', mew=0.5, ms=5, alpha=0.3, label='$y=1$') ax.plot(X[~y_is_1, 1], X[~y_is_1, 0], 'ko', ls='None', mew=0.5, ms=5, fillstyle='none', alpha=0.3, label='$y=0$') ax.plot(x_plt[indx_star, 1], x_plt[indx_star, 0], 'r+', mew=1, ms=5, label='$\mathbf{x}_*$') ax.set_xlabel('$x_1$') ax.set_ylabel('$x_2$') ax.set_yticks([-1.0, -0.5, 0.0, 0.5, 1.0]) ax.legend(loc='lower left', bbox_to_anchor=(3.8, 0.5)) # Posterior under a 1 observation ax = fig.add_subplot(132) axs.append(ax) ax.contourf(yv, xv, P1.detach().numpy().reshape(ngrid, ngrid), levels=np.linspace(0, 1, 20), alpha=0.2) ax.set_title('Look-ahead posterior\n$\pi(\mathbf{x} | \mathcal{D}_{n+1}(\mathbf{x}_*, y_*=1))$') ax.plot(X[y_is_1, 1], X[y_is_1, 0], 'kx', ls='None', mew=0.5, ms=5, alpha=0.3) ax.plot(X[~y_is_1, 1], X[~y_is_1, 0], 'ko', ls='None', mew=0.5, ms=5, fillstyle='none', alpha=0.3) ax.plot(x_plt[indx_star, 1], x_plt[indx_star, 0], 'rx', ls='None', mew=1, ms=5) ax.set_xlabel('$x_1$') ax.set_yticks([-1.0, -0.5, 0.0, 0.5, 1.0]) ax.set_yticklabels([]) # Posterior under a 0 observation ax = fig.add_subplot(133) axs.append(ax) cf = ax.contourf(yv, xv, P0.detach().numpy().reshape(ngrid, ngrid), levels=np.linspace(0, 1, 20), alpha=0.2) ax.set_title('Look-ahead posterior\n$\pi(\mathbf{x} | \mathcal{D}_{n+1}(\mathbf{x}_*, y_*=0))$') ax.plot(X[y_is_1, 1], X[y_is_1, 0], 'kx', ls='None', mew=0.5, ms=5, alpha=0.3) ax.plot(X[~y_is_1, 1], X[~y_is_1, 0], 'ko', ls='None', mew=0.5, ms=5, fillstyle='none', alpha=0.3) ax.plot(x_plt[indx_star, 1], x_plt[indx_star, 0], 'ro', ls='None', mew=1, ms=5, fillstyle='none') ax.set_xlabel('$x_1$') ax.set_yticks([-1.0, -0.5, 0.0, 0.5, 1.0]) ax.set_yticklabels([]) fig.subplots_adjust(bottom=0.2, left=0.13, top=0.8, right=0.85) fig.colorbar(cf, ax=axs, ticks=[0, 0.25, 0.5, 0.75, 1.0], pad=0.01) plt.savefig('pdfs/posteriors.pdf', pad_inches=0) if __name__ == '__main__': make_figure()
bernoulli_lse-main
figures/plot_posteriors.py
# Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. import numpy as np import pandas as pd from pathlib import Path import matplotlib.pyplot as plt import sys sys.path.append("..") from plot_config import * # need cameraready for original thresh rundata = list(Path("../data/cameraready/").glob("*out.csv")) sensitivity_rundata = list(Path("../data/thresh_sensitivity").glob("*out.csv")) def compile_results(): orig_dfiles = pd.concat([pd.read_csv(d) for d in rundata]) thresh_dfiles = pd.concat([pd.read_csv(d) for d in sensitivity_rundata]) # the hartmann thresh=0.5 runs were duplicated so drop them idx = np.logical_not(np.logical_and( thresh_dfiles.problem == "hartmann6_binary", thresh_dfiles.opt_strat_target == 0.5, )) df = pd.concat([orig_dfiles, thresh_dfiles[idx]]) df["method"] = df.OptimizeAcqfGenerator_acqf.fillna("Quasi-random").astype( "category" ) df["method"] = df.method.cat.rename_categories( { "MCLevelSetEstimation": "Straddle", "MCPosteriorVariance": "BALV", # comment me out for largerun "BernoulliMCMutualInformation": "BALD", # comment me out for largerun } ) df = df[df.final == True] acqfs = [ "EAVC", "LocalMI", "GlobalMI", ] problems = list(df["problem"].unique()) problems.remove("discrim_lowdim") res = {prob: {} for prob in problems} for levels, g in df.groupby(["method", "rep", "problem", "opt_strat_target"]): method, _, prob, target = levels if method in acqfs and prob in problems: acqf = f"{method}_{target}" if acqf not in res[prob]: res[prob][acqf] = [] res[prob][acqf].append(g["brier"].item()) for prob in problems: for acqf in res[prob]: res[prob][acqf] = np.array(res[prob][acqf]) return res def make_thresh_sensitivity_figure(): res = compile_results() methods = [ "EAVC", "LocalMI", "GlobalMI", #"Quasi-random" # Not run for this study ] # Make the plot fig = plt.figure(figsize=(6.75, 2.3)) prob = "hartmann6_binary" threshes = [0.5, 0.65, 0.95] ax = fig.add_subplot(131) for i, method in enumerate(methods): ymean = [res[prob][f"{method}_{thresh}"].mean() for thresh in threshes] yerr = [ 2 * res[prob][f"{method}_{thresh}"].std() / np.sqrt(len(res[prob][f"{method}_{thresh}"])) for thresh in threshes ] color, ls = method_styles[method] ax.errorbar( threshes, ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls, ) ax.plot(threshes, ymean, color=color, ls=ls, label=method, lw=1) ax.set_xticks(threshes) ax.grid(alpha=0.1) ax.set_title("Binarized Hartmann6 (6-d)") ax.set_xlabel("Target threshold") ax.set_ylabel("Final Brier Score\nafter 750 iterations") ax.legend(loc="lower left", bbox_to_anchor=(0.9, -0.63), ncol=3) prob = "discrim_highdim" ax = fig.add_subplot(132) threshes = [0.65, 0.75, 0.95] for i, method in enumerate(methods): ymean = [res[prob][f"{method}_{thresh}"].mean() for thresh in threshes] yerr = [ 2 * res[prob][f"{method}_{thresh}"].std() / np.sqrt(len(res[prob][f"{method}_{thresh}"])) for thresh in threshes ] color, ls = method_styles[method] ax.errorbar( threshes, ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls, ) ax.plot(threshes, ymean, color=color, ls=ls, label=method, lw=1) ax.set_xticks(threshes) ax.grid(alpha=0.1) ax.set_title("Psych. Discrimination (8-d)") ax.set_xlabel("Target threshold") prob = "contrast_sensitivity_6d" ax = fig.add_subplot(133) for i, method in enumerate(methods): ymean = [res[prob][f"{method}_{thresh}"].mean() for thresh in threshes] yerr = [ 2 * res[prob][f"{method}_{thresh}"].std() / np.sqrt(len(res[prob][f"{method}_{thresh}"])) for thresh in threshes ] color, ls = method_styles[method] ax.errorbar( threshes, ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls, ) ax.plot(threshes, ymean, color=color, ls=ls, label=method, lw=1) ax.set_xticks(threshes) ax.grid(alpha=0.1) ax.set_xlabel("Target threshold") ax.set_title("Contrast Sensitivity (6-d)") fig.subplots_adjust(bottom=0.34, left=0.09, top=0.91, right=0.99, wspace=0.3) plt.savefig("pdfs/thresh_sensitivity.pdf", pad_inches=0) if __name__ == "__main__": make_thresh_sensitivity_figure()
bernoulli_lse-main
figures/plot_thresh_sensitivity_results.py
# Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. import pickle import matplotlib.pyplot as plt import numpy as np import sys sys.path.append('..') from plot_config import * from plot_experiment_results import compile_results, run_data def make_figure(): res, itrs = compile_results(run_data) edge_samp = res['prop_edge_sampling_mean'] # Make the plot fig = plt.figure(figsize=(6.75, 3)) prob = 'hartmann6_binary' methods = list(method_styles.keys())[:8] ax = fig.add_subplot(131) for i, model_name in enumerate(methods): y = edge_samp[prob][model_name][:,-1].mean() n = edge_samp[prob][model_name].shape[0] yerr = 2 * edge_samp[prob][model_name][:,-1].std() / np.sqrt(n) ax.bar([i], [y], yerr=yerr, color=method_styles[model_name][0]) print(model_name, y) ax.set_xticks(range(8)) ax.set_xticklabels(methods, rotation=90) ax.set_ylim([0, 1]) ax.set_yticks([0, 0.25, 0.5, 0.75, 1]) ax.set_yticklabels(['0\%', '25\%', '50\%', '75\%', '100\%']) ax.grid(alpha=0.1) ax.set_ylabel('Proportion of samples near edge') ax.set_title('Binarized Hartmann6') prob = 'discrim_lowdim' ax = fig.add_subplot(132) for i, model_name in enumerate(methods): y = edge_samp[prob][model_name][:,-1].mean() n = edge_samp[prob][model_name].shape[0] yerr = 2 * edge_samp[prob][model_name][:,-1].std() / np.sqrt(n) ax.bar([i], [y], yerr=yerr, color=method_styles[model_name][0]) ax.set_xticks(range(8)) ax.set_xticklabels(methods, rotation=90) ax.set_ylim([0, 1]) ax.set_yticks([0, 0.25, 0.5, 0.75, 1]) ax.set_yticklabels([]) ax.grid(alpha=0.1) ax.set_title('Psych. Discrimination (2-d)') prob = 'discrim_highdim' ax = fig.add_subplot(133) for i, model_name in enumerate(methods): y = edge_samp[prob][model_name][:,-1].mean() n = edge_samp[prob][model_name].shape[0] yerr = 2 * edge_samp[prob][model_name][:,-1].std() / np.sqrt(n) ax.bar([i], [y], yerr=yerr, color=method_styles[model_name][0]) ax.set_xticks(range(8)) ax.set_xticklabels(methods, rotation=90) ax.set_ylim([0, 1]) ax.set_yticks([0, 0.25, 0.5, 0.75, 1]) ax.set_yticklabels([]) ax.grid(alpha=0.1) ax.set_title('Psych. Discrimination (8-d)') fig.subplots_adjust(bottom=0.34, left=0.08, top=0.93, right=0.99, wspace=0.1) plt.savefig('pdfs/edge_sampling.pdf', pad_inches=0) if __name__ == '__main__': make_figure()
bernoulli_lse-main
figures/plot_edge_sampling.py
# Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. import numpy as np import pandas as pd import sys from pathlib import Path import matplotlib.pyplot as plt sys.path.append('..') from plot_config import * import re run_data = list(Path("../data/cameraready/").glob("*out.csv")) def compile_results(run_data): dfiles = [pd.read_csv(d) for d in run_data] df = pd.concat(dfiles) df["method"] = df.OptimizeAcqfGenerator_acqf.fillna("Quasi-random").astype( "category" ) df["method"] = df.method.cat.rename_categories( { "MCLevelSetEstimation": "Straddle", "MCPosteriorVariance": "BALV", "BernoulliMCMutualInformation": "BALD", } ) df = df[df.final==False] acqfs = [ "Straddle", "EAVC", "LocalMI", "GlobalMI", "LocalSUR", "GlobalSUR", "ApproxGlobalSUR", "Quasi-random", "BALD", "BALV", ] metrics = [ "brier", "class_errors", "prop_edge_sampling_mean", "fit_time", "gen_time", ] problems = df["problem"].unique() res = { metric: {prob: {acqf: [] for acqf in acqfs} for prob in problems} for metric in metrics } itrs = {} for _, g in df.groupby(["method", "rep", "problem"]): prob = g["problem"].values[0] if prob in itrs: if not np.array_equal(g["trial_id"].values, itrs[prob]): raise Exception else: itrs[prob] = g["trial_id"].values acqf = g["method"].values[0] for metric in metrics: res[metric][prob][acqf].append(g[metric]) for metric in metrics: for prob in problems: for acqf in acqfs: res[metric][prob][acqf] = np.array(res[metric][prob][acqf]) return res, itrs def make_benchmark_figure(): res, itrs = compile_results(run_data) # Make the plot fig = plt.figure(figsize=(6.75, 2.3)) # Plot each problem metric = "brier" prob = "hartmann6_binary" methods = list(method_styles.keys())[:8] # prob = 'Binarized\n Hartmann6' ax = fig.add_subplot(131) for method in methods: y = res[metric][prob][method] ymean = np.mean(y, axis=0) yerr = 2 * np.std(y, axis=0) / np.sqrt(y.shape[0]) color, ls = method_styles[method] ax.errorbar(itrs[prob], ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls) ax.plot(itrs[prob], ymean, lw=1, label=method, color=color, ls=ls) ax.set_xlim([0, 760]) ax.set_xticks([0, 250, 500, 750]) ax.set_ylim([0.15, 0.5]) ax.set_yticks([0.2, 0.3, 0.4, 0.5]) ax.grid(alpha=0.1) ax.set_title("Binarized Hartmann6 (6-d)") ax.set_xlabel("Iteration") ax.set_ylabel("Brier Score") ax.legend(loc="lower left", bbox_to_anchor=(0.45, -0.63), ncol=4) prob = "discrim_lowdim" ax = fig.add_subplot(132) for method in methods: y = res[metric][prob][method] ymean = np.mean(y, axis=0) yerr = 2 * np.std(y, axis=0) / np.sqrt(y.shape[0]) color, ls = method_styles[method] ax.errorbar(itrs[prob], ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls) ax.plot(itrs[prob], ymean, lw=1, label=method, color=color, ls=ls) ax.set_xlim([0, 510]) ax.set_ylim([0.01, 0.10]) ax.set_yticks([0.02, 0.04, 0.06, 0.08, 0.1]) ax.set_xticks([0, 100, 200, 300, 400, 500]) ax.grid(alpha=0.1) ax.set_title("Psych. Discrimination (2-d)") ax.set_xlabel("Iteration") prob = "discrim_highdim" ax = fig.add_subplot(133) for method in methods: y = res[metric][prob][method] ymean = np.mean(y, axis=0) yerr = 2 * np.std(y, axis=0) / np.sqrt(y.shape[0]) color, ls = method_styles[method] ax.errorbar(itrs[prob], ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls) ax.plot(itrs[prob], ymean, lw=1, label=method, color=color, ls=ls) ax.set_xlim([0, 760]) ax.set_xticks([0, 250, 500, 750]) ax.set_ylim([0.1, 0.5]) ax.set_yticks([0.1, 0.2, 0.3, 0.4, 0.5]) ax.grid(alpha=0.1) ax.set_title("Psych. Discrimination (8-d)") ax.set_xlabel("Iteration") fig.subplots_adjust(bottom=0.34, left=0.065, top=0.91, right=0.99, wspace=0.2) plt.savefig("pdfs/benchmarks.pdf", pad_inches=0) def make_realworld_figure(): res, itrs = compile_results(run_data) # Make the plot fig = plt.figure(figsize=(3.25, 3)) ax = fig.add_subplot(111) prob = "contrast_sensitivity_6d" metric = "brier" methods = list(method_styles.keys())[:8] for method in methods: y = res[metric][prob][method] ymean = np.mean(y, axis=0) yerr = 2 * np.std(y, axis=0) / np.sqrt(y.shape[0]) color, ls = method_styles[method] ax.errorbar(itrs[prob], ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls) ax.plot(itrs[prob], ymean, lw=1, label=method, color=color, ls=ls) ax.set_xlim([0, 760]) ax.set_xticks([0, 250, 500, 750]) ax.set_ylim([0.1, 0.6]) ax.set_yticks([0.1, 0.2, 0.3, 0.4, 0.5, 0.6]) ax.grid(alpha=0.1) ax.set_title("Contrast Sensitivity (6-d)") ax.set_xlabel("Iteration") ax.set_ylabel("Brier Score") ax.legend(ncol=2, loc="lower left", bbox_to_anchor=[0.05, -0.7]) fig.subplots_adjust(bottom=0.38, left=0.13, top=0.93, right=0.98) plt.savefig("pdfs/realworld.pdf", pad_inches=0) if __name__ == "__main__": make_benchmark_figure() make_realworld_figure()
bernoulli_lse-main
figures/plot_experiment_results.py
# Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. import numpy as np import pandas as pd import matplotlib.pyplot as plt import sys sys.path.append("..") from pathlib import Path from plot_config import * # need cameraready for init=10 rundata = list(Path("../data/cameraready/").glob("*out.csv")) + list(Path("../data/init_sensitivity").glob("*out.csv")) def compile_results(): dfiles = [pd.read_csv(d) for d in rundata] df = pd.concat(dfiles) df["method"] = df.OptimizeAcqfGenerator_acqf.fillna("Quasi-random").astype( "category" ) df["method"] = df.method.cat.rename_categories( { "MCLevelSetEstimation": "Straddle", "MCPosteriorVariance": "BALV", "BernoulliMCMutualInformation": "BALD", } ) df = df[df.final == True] acqfs = [ "EAVC", "LocalMI", "GlobalMI", "Quasi-random", ] problems = list(df["problem"].unique()) problems.remove("discrim_lowdim") res = {prob: {} for prob in problems} for levels, g in df.groupby(["method", "rep", "problem", "init_strat_n_trials"]): method, _, prob, n_init = levels if method in acqfs and prob in problems: acqf = f"{method}_{n_init}" if acqf not in res[prob]: res[prob][acqf] = [] res[prob][acqf].append(g["brier"].item()) for prob in problems: for acqf in res[prob]: res[prob][acqf] = np.array(res[prob][acqf]) return res def make_init_sensitivity_figure(): res = compile_results() ninits = [10, 100, 250, 500] # Make the plot fig = plt.figure(figsize=(6.75, 2.3)) prob = "hartmann6_binary" ax = fig.add_subplot(131) methods = [ "EAVC", "LocalMI", "GlobalMI", ] for i, method in enumerate(methods): ymean = [res[prob][f"{method}_{ninit}"].mean() for ninit in ninits] yerr = [ 2 * res[prob][f"{method}_{ninit}"].std() / np.sqrt(len(res[prob][f"{method}_{ninit}"])) for ninit in ninits ] color, ls = method_styles[method] ax.errorbar( ninits, ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls, ) ax.plot(ninits, ymean, lw=1, label=method, color=color, ls=ls) # Add Sobol ymean = [res[prob]["Quasi-random_10"].mean() for ninit in ninits] yerr = [ 2 * res[prob]["Quasi-random_10"].std() / np.sqrt(len(res[prob]["Quasi-random_10"])) for ninit in ninits ] color, ls = method_styles["Quasi-random"] ax.errorbar( ninits, ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls, ) ax.plot(ninits, ymean, lw=1, label="Quasi-random", color=color, ls=ls) ax.set_xticks(ninits) ax.set_ylim([0.15, 0.3]) ax.set_yticks([0.15, 0.2, 0.25, 0.3]) ax.grid(alpha=0.1) ax.set_title("Binarized Hartmann6 (6-d)") ax.set_xlabel("Initial design size") ax.set_ylabel("Final Brier Score\nafter 750 iterations") ax.legend(loc="lower left", bbox_to_anchor=(0.45, -0.63), ncol=4) prob = "discrim_highdim" ax = fig.add_subplot(132) for i, method in enumerate(methods): ymean = [res[prob][f"{method}_{ninit}"].mean() for ninit in ninits] yerr = [ 2 * res[prob][f"{method}_{ninit}"].std() / np.sqrt(len(res[prob][f"{method}_{ninit}"])) for ninit in ninits ] color, ls = method_styles[method] ax.errorbar( ninits, ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls, ) ax.plot(ninits, ymean, lw=1, label=method, color=color, ls=ls) # Add Sobol ymean = [res[prob]["Quasi-random_10"].mean() for ninit in ninits] yerr = [ 2 * res[prob]["Quasi-random_10"].std() / np.sqrt(len(res[prob]["Quasi-random_10"])) for ninit in ninits ] color, ls = method_styles["Quasi-random"] ax.errorbar( ninits, ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls, ) ax.plot(ninits, ymean, lw=1, label="Quasi-random", color=color, ls=ls) ax.set_xticks(ninits) ax.grid(alpha=0.1) ax.set_title("Psych. Discrimination (8-d)") ax.set_xlabel("Initial design size") ax.set_ylim([0.1, 0.4]) ax.set_yticks([0.1, 0.2, 0.3, 0.4]) prob = "contrast_sensitivity_6d" ax = fig.add_subplot(133) for i, method in enumerate(methods): ymean = [res[prob][f"{method}_{ninit}"].mean() for ninit in ninits] yerr = [ 2 * res[prob][f"{method}_{ninit}"].std() / np.sqrt(len(res[prob][f"{method}_{ninit}"])) for ninit in ninits ] color, ls = method_styles[method] ax.errorbar( ninits, ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls, ) ax.plot(ninits, ymean, lw=1, label=method, color=color, ls=ls) # Add Sobol ymean = [res[prob]["Quasi-random_10"].mean() for ninit in ninits] yerr = [ 2 * res[prob]["Quasi-random_10"].std() / np.sqrt(len(res[prob]["Quasi-random_10"])) for ninit in ninits ] color, ls = method_styles["Quasi-random"] ax.errorbar( ninits, ymean, yerr=yerr, lw=1, alpha=0.3, color=color, ls=ls, ) ax.plot(ninits, ymean, lw=1, label="Quasi-random", color=color, ls=ls) ax.set_xticks(ninits) ax.set_ylim([0.12, 0.25]) ax.set_yticks([0.12, 0.15, 0.18, 0.21, 0.24]) ax.grid(alpha=0.1) ax.set_xlabel("Initial design size") ax.set_title("Contrast Sensitivity (6-d)") fig.subplots_adjust(bottom=0.34, left=0.093, top=0.91, right=0.99, wspace=0.3) plt.savefig("pdfs/init_sensitivity.pdf", pad_inches=0) if __name__ == "__main__": make_init_sensitivity_figure()
bernoulli_lse-main
figures/plot_init_sensitivity_results.py
# Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. from datetime import datetime """ Develop an experiment that measures and combination of the following features: spatial_frequency temporal_frequency mean_luminance eccentricity field_angle orientation """ constants = dict( savefolder="./databases/", timestamp=datetime.now().strftime("%Y-%m-%d_%H-%M-%S"), config_path="./config_8d.ini", seed=1, ) base_params = { "spatial_frequency": 2, "orientation": 0, "pedestal": 0.5, "contrast": 0.75, "temporal_frequency": 0, "size": 10, "angle_dist":0, "eccentricity": 0, } psychopy_vars = dict( setSizePix=[1680, 1050], setWidth=47.475, setDistance=57, pre_duration_s=0.0, stim_duration_s=5.0, post_duration_s=1, response_wait=2, iti=0, )
bernoulli_lse-main
human_data_collection/config.py
# Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. import numpy as np import torch from aepsych.server import AEPsychServer from psychopy import core, data, event, gui, monitors, visual from contrast_discrimination import config from contrast_discrimination.helpers import HalfGrating class ServerHelper: def __init__(self, config_path, db_path): self._server = AEPsychServer(database_path=db_path) with open(config_path, "r") as f: configstr = f.read() self._server.handle_setup_v01( {"type": "setup", "message": {"config_str": configstr}} ) def ask(self): request = {"message": "", "type": "ask"} return self._server.handle_ask_v01(request)["config"] def tell(self, config, outcome): request = { "type": "tell", "message": {"config": config, "outcome": outcome}, } self._server.handle_tell(request) def run_experiment(): seed = config.constants["seed"] config_path = config.constants["config_path"] torch.manual_seed(seed) np.random.seed(seed) expInfo = {} expInfo["dateStr"] = data.getDateStr() # add the current time # present a dialogue to change params dlg = gui.DlgFromDict(expInfo, title="multi-D JND Exp", fixed=["dateStr"]) if not dlg.OK: core.quit() # the user hit cancel so exit # where to save data fileName = "../data/csf_dataset" screen = monitors.Monitor("testMonitor", gamma=1) screen.setSizePix(config.psychopy_vars["setSizePix"]) screen.setWidth(config.psychopy_vars["setWidth"]) screen.setDistance(config.psychopy_vars["setDistance"]) win = visual.Window( allowGUI=True, units="deg", monitor=screen, bpc=(8, 8, 8), size=config.psychopy_vars["setSizePix"], fullscr=False, ) screen_text_g = visual.TextStim(win, text=None, alignHoriz="center", color="green") screen_text_r = visual.TextStim(win, text=None, alignHoriz="center", color="red") screen_text = visual.TextStim(win, text=None, alignHoriz="center", color="gray") # display instructions and wait message2 = visual.TextStim( win, pos=[0, +3], text="Hit the space bar key when ready and " "to advance to the next trial after you see a red cross.", ) message1 = visual.TextStim( win, pos=[0, -3], text="You'll see a stimulus. One side will have a grating and the other will be noise." " " "Press left or right corresponding to the side with noise. If you don't know, please guess.", ) message1.draw() message2.draw() win.flip() # to show our newly drawn 'stimuli' # pause until there's a keypress event.waitKeys() # start the trial: draw grating clock = core.Clock() screen_text_r.setText( "+" ) # this should update the fixation with the new background color, but it isnt for some reason screen_text_r.draw(win=win) win.flip() server_helper = ServerHelper(config_path=config_path, db_path=f"{fileName}.db") # create stimulus stim = HalfGrating(**config.base_params, win=win) i = 0 while not server_helper._server.strat.finished: trial_params = server_helper.ask() stim.update(trial_params) bg_color = np.array([stim.pedestal_psychopy_scale] * 3) win.setColor(bg_color) win.color = bg_color win.flip() screen_text_r.setText("+") screen_text_r.draw(win=win) win.flip() fixation_keys = [] while not fixation_keys: fixation_keys = event.getKeys(keyList=["space"]) if "space" in fixation_keys: screen_text.setText("+") screen_text.draw(win=win) win.flip() noisy_half = "left" if np.random.randint(2) == 0 else "right" clock.reset() keys = stim.draw( noisy_half=noisy_half, win=win, pre_duration_s=config.psychopy_vars["pre_duration_s"], stim_duration_s=config.psychopy_vars["stim_duration_s"], ) response = noisy_half in keys win.flip() if response: screen_text_g.setText("Correct") screen_text_g.draw() win.flip() else: screen_text_r.setText("Incorrect") screen_text_r.draw() win.flip() server_helper.tell(trial_params, response) event.clearEvents() i = i + 1 win.close() pd_df = server_helper._server.get_dataframe_from_replay() pd_df.to_csv(fileName + ".csv", index=False) core.quit() if __name__ == "__main__": run_experiment()
bernoulli_lse-main
human_data_collection/experiment.py
# Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. import numpy as np from psychopy.visual.image import ImageStim from psychopy import core, event import pyglet pyglet.options["debug_gl"] = False GL = pyglet.gl def polar_to_cartesian(r, theta): z = r * np.exp(1j * np.radians(theta)) return z.real, z.imag def cartesian_to_polar(x, y): z = x + 1j * y return (np.abs(z), np.angle(z, deg=True)) class AnimatedGrating: param_transforms = {"contrast": lambda x: 10 ** x, "pedestal": lambda x: 10 ** x} def __init__( self, spatial_frequency, orientation, pedestal, contrast, temporal_frequency, eccentricity, size, angle_dist, win, cpd=60, # display cycles per degree Lmin=0, # min luminance in nits Lmax=255, # max luminance in nits res=256, # texture resolution noisy=False, *args, **kw, ): self.spatial_frequency = spatial_frequency self.temporal_frequency = temporal_frequency self.orientation = orientation self.pedestal = pedestal self.contrast = contrast self.settable_params = ( "spatial_frequency", "temporal_frequency", "orientation", "pedestal", "contrast", "size", "eccentricity", "angle_dist", ) self.cpd = cpd self.Lmin = Lmin self.Lmax = Lmax self.res = res self.noisy = noisy self.initial_phase = np.random.uniform(low=0, high=0.2, size=(1)) img = np.zeros((self.res, self.res)) self.win = win self._stim = ImageStim(image=img, mask="gauss", win=win, *args, **kw) # these get set on _stim self.size = size self.eccentricity = eccentricity self.angle_dist = angle_dist def update(self, trial_config): for k, v in trial_config.items(): if k in self.settable_params: if k in self.param_transforms: setattr(self, k, self.param_transforms[k](v[0])) else: setattr(self, k, v[0]) @property def size(self): return self._stim.size @size.setter def size(self, x): self._stim.size = x @property def eccentricity(self): return cartesian_to_polar(*self._stim.pos)[0] @eccentricity.setter def eccentricity(self, x): current_coords = cartesian_to_polar(*self._stim.pos) self._stim.pos = polar_to_cartesian(x, current_coords[1]) @property def angle_dist(self): return cartesian_to_polar(*self._stim.pos)[1] @angle_dist.setter def angle_dist(self, deg): current_coords = cartesian_to_polar(*self._stim.pos) self._stim.pos = polar_to_cartesian(current_coords[0], deg + 90) @property def pedestal_psychopy_scale(self): return self.pedestal * 2 - 1 def draw( self, noisy=False, win=None, pre_duration_s=0.1, stim_duration_s=5.0, *args, **kwargs, ): win = win or self.win clock = core.Clock() clock.reset() self._stim.image = self.get_texture(self.initial_phase, noisy=noisy) while clock.getTime() < pre_duration_s: win.flip() start_time = clock.getTime() while clock.getTime() < pre_duration_s + stim_duration_s: if self.temporal_frequency > 0: newphase = (clock.getTime() - start_time) * self.temporal_frequency self._stim.image = self.get_texture( newphase + self.initial_phase, noisy=noisy ) self._stim.draw() def get_texture(self, phase=0, noisy=False): pedestal_lum = self.pedestal * (self.Lmax - self.Lmin) + self.Lmin grating_max = (self.contrast * (2 * pedestal_lum + self.Lmin) + self.Lmin) / 2 x = np.arange(0, self.res) / self.cpd + phase y = np.arange(0, self.res) / self.cpd + phase x_grid, y_grid = np.meshgrid(x, y) wave = x_grid * np.cos(np.radians(self.orientation)) + y_grid * np.sin( np.radians(self.orientation) ) scaled_imag_wave = 1j * 2 * np.pi * self.spatial_frequency * wave img = grating_max * np.real(np.exp(scaled_imag_wave)) + pedestal_lum # convert from luminance to values in [-1, 1] as psychopy wants img = img / ((self.Lmax - self.Lmin) / 2) - 1 if noisy: flatimg = img.flatten() np.random.shuffle(flatimg) img = flatimg.reshape(self.res, self.res) return img class HalfGrating(AnimatedGrating): def noisify_half_texture(self, img, noisy_half): img = img.T # transpose so our indexing tricks work flatimg = img.flatten() if noisy_half == "left": noisy = flatimg[: (self.res ** 2) // 2] np.random.shuffle(noisy) img = np.r_[noisy, flatimg[(self.res ** 2) // 2 :]].reshape( self.res, self.res ) else: noisy = flatimg[(self.res ** 2) // 2 :] np.random.shuffle(noisy) img = np.r_[flatimg[: (self.res ** 2) // 2], noisy].reshape( self.res, self.res ) return img.T # untranspose def get_texture(self, phase, noisy_half): img = super().get_texture(phase, noisy=False) img = self.noisify_half_texture(img, noisy_half) return img def draw( self, noisy_half="left", win=None, pre_duration_s=0.1, *args, **kwargs, ): win = win or self.win clock = core.Clock() clock.reset() event.clearEvents() self._stim.image = self.get_texture(self.initial_phase, noisy_half=noisy_half) while clock.getTime() < pre_duration_s: win.flip() start_time = clock.getTime() while True: if self.temporal_frequency > 0: newphase = (clock.getTime() - start_time) * self.temporal_frequency self._stim.image = self.get_texture( newphase + self.initial_phase, noisy_half=noisy_half ) self._stim.draw() keys = event.getKeys(keyList=["left", "right"]) win.flip() if len(keys) > 0: return keys return keys class ExperimentAborted(Exception): pass class QuitHelper: """Helper to quit the experiment by pressing a key twice within 500ms. It quits by simply raising 'ExperimentAborted'. This is necessary because from the separate thread that psychopy checks its global key events in, you cannot raise an Exception in the main thread. """ def __init__(self): self.quit_requested = False self.debounce_timestamp = None def request_quit(self): """Must be called twice in 500ms to set a flag that causes ExperimentAborted to be raised when quit_if_requested is called. This indirection is needed if request_quit is called from a separate thread (as with psychopy global event keys) """ tprev = self.debounce_timestamp tnow = core.getTime() if not tprev is None and tnow - tprev < 0.5: self.quit_requested = True self.debounce_timestamp = tnow def quit_if_requested(self): """Raises ExperimentAborted if request_quit has been called twice in 500ms""" if self.quit_requested: raise ExperimentAborted return True
bernoulli_lse-main
human_data_collection/helpers.py
# Copyright (c) Meta Platforms, Inc. and affiliates. from data import NL2BashDataset from collectors import CollectorWithInfo import argparse if __name__ == "__main__": dataset = NL2BashDataset() parser = argparse.ArgumentParser() parser.add_argument("--num_seeds", type=int, default=25) parser.add_argument("--num_samples", type=int, default=5) args = CollectorWithInfo.parse_args(parser) args.dataset = "nl2bash" args.seed = list(range(args.num_seeds)) args.prompt_template = "<text>{src}</text>\n<code>{trg}</code>\n" args.example_template = "<text>{src}</text>\n<code>" collector = CollectorWithInfo.from_args(args, dataset) for i in range(args.num_samples): collector(i, i, 5)
coder_reviewer_reranking-main
collect_nl2bash.py
# Copyright (c) Meta Platforms, Inc. and affiliates. from pathlib import Path import os from glob import glob from argparse import ArgumentParser import html import json from utils import * from tqdm import tqdm, trange from data import HumanEvalDataset, rindex, extract_docstring from functools import partial from pyminifier_canonicalize import clean_comment, remove_print def postprocess_func_only(code, tokens): lines = [] for line in code.split("\n"): if len(line.strip()) > 0 and not line.startswith(" "): continue else: lines.append(line) code = "\n".join(lines) code = code.rstrip() curr = "" for i, tok in enumerate(tokens): curr += tok if len(curr) >= len(code): break return code, tokens[: i + 1] def make_new_context( codex_data, problem, canonicalize=False, clean_print=False, ): prompt = codex_data["prompt"] code_sample = codex_data["trg_prediction"] if canonicalize: try: code_sample = clean_comment(code_sample) except: # static error code_sample = code_sample if clean_print: code_sample = remove_print(code_sample) func_name = problem["entry_point"] docstring, func_header, func_context, doc_start = extract_docstring(prompt) if canonicalize: func_header = func_header.replace(f"{func_name}(", "f(") docstring = docstring.replace(f"{func_name}(", "f(") code_sample = code_sample.replace(f"{func_name}(", "f(") reverse_prompt = "\n\n# write the docstring for the above function\n" without_ref = ( func_context + "\n" + func_header.strip() + "\n" + code_sample + reverse_prompt + func_header.strip() + "\n" + f" {doc_start}" ) with_ref = without_ref + docstring.strip()[3:] return with_ref.rstrip(), without_ref def rindex(lst, value): return len(lst) - lst[::-1].index(value) - 1 def find_start(tokens): tokens = tokens[:-2] # remove last docstring marker for marker in [' """', " '''", ' ""', "''"]: if marker in tokens: return rindex(tokens[:-1], marker) + 1 raise ValueError("not found") def batch_query_reverse_logp(all_codex_data, args): for outer_i, batch_start in enumerate( range(0, len(all_codex_data), args.batch_size) ): batch_data = all_codex_data[batch_start : batch_start + args.batch_size] batch_prompts = [] batch_data_with_prompt = [] for codex_data, problem in batch_data: # TODO: postprocessing, should move else where codex_data["trg_prediction"], codex_data["tokens"] = postprocess_func_only( codex_data["trg_prediction"], codex_data["tokens"] ) codex_data["logprobs"] = codex_data["logprobs"][: len(codex_data["tokens"])] with_ref_prompt, without_ref_prompt = make_new_context( codex_data, problem, canonicalize=args.canonicalize, clean_print=args.clean_print, ) batch_prompts.append(with_ref_prompt) batch_data_with_prompt.append( (codex_data, problem, with_ref_prompt, without_ref_prompt) ) with_ref_reponse, _ = safe_codex_call( args, batch_prompts, temperature=1.0, echo=True, max_tokens=0, api_i=(outer_i % 3), ) for ( batch_i, (codex_data, problem, with_ref_prompt, without_ref_prompt), ) in enumerate(batch_data_with_prompt): num_api_tokens = find_start( with_ref_reponse["choices"][batch_i]["logprobs"]["tokens"] ) gt_prompt_logprob = with_ref_reponse["choices"][batch_i]["logprobs"][ "token_logprobs" ][num_api_tokens:] gt_prompt_tokens = with_ref_reponse["choices"][batch_i]["logprobs"][ "tokens" ][num_api_tokens:] codex_data["reverse_prompt_with_ref"] = with_ref_prompt codex_data["reverse_prompt_without_ref"] = without_ref_prompt codex_data["prompt_reverse_logprobs"] = gt_prompt_logprob codex_data["prompt_reverse_tokens"] = gt_prompt_tokens codex_data["prompt_reverse_full_tokens"] = with_ref_reponse["choices"][ batch_i ]["logprobs"]["tokens"] codex_data["prompt_reverse_full_logprobs"] = with_ref_reponse["choices"][ batch_i ]["logprobs"]["token_logprobs"] all_codex_data = [d[0] for d in all_codex_data] return all_codex_data if __name__ == "__main__": parser = ArgumentParser() parser.add_argument("--model", type=str, default="codex001") parser.add_argument( "--dataset", type=str, default="humaneval", choices=["humaneval", "codet_humaneval", "mbpp_sanitized"], ) parser.add_argument("--tag", type=str, default="") parser.add_argument("--split", type=str, default="test") parser.add_argument("--num_samples", type=int, default=5) parser.add_argument("--num_procs", type=int, default=40) parser.add_argument( "--data_path", type=str, default="./samples/codex002", ) parser.add_argument("--temperature", type=float, default=0.3) parser.add_argument("--max_tokens", type=int, default=512) parser.add_argument("--batch_size", type=int, default=20) parser.add_argument("--top_p", type=float, default=1.0) parser.add_argument("--canonicalize", default=False, action="store_true") parser.add_argument("--clean-print", default=False, action="store_true") parser.add_argument("--overwrite-output-dir", default=False, action="store_true") args = parser.parse_args() args.data_path = Path(args.data_path) out_dir = f"seed-*/**/*-{args.temperature}" if args.top_p != 1.0: out_dir += f"-p{args.top_p}" if args.max_tokens != 512: out_dir += f"-max{args.max_tokens}" args.data_path = args.data_path / args.dataset / out_dir paths = list(sorted(glob(str(args.data_path), recursive=True))) if args.dataset == "codet_humaneval": dataset = HumanEvalDataset( "dataset/human_eval/dataset/CodeTHumanEval.jsonl", mode="prompt_only" ) else: dataset = HumanEvalDataset( path="dataset/mbpp/mbpp_sanitized_for_code_generation.jsonl", mode="prompt_only", ) prompt_to_data = {p["prompt"]: p for task_id, p in dataset.raw_data.items()} paths = sorted(paths) for path in tqdm(paths, desc="total seeds", disable=False): path = Path(path) for sample_i in trange(args.num_samples): if len(args.tag) == 0: output_file_name = f"{args.split}-{sample_i}.jsonl" else: output_file_name = f"{args.split}-{sample_i}-{args.tag}.jsonl" try: all_codex_data = [] with open(path / f"{args.split}-{sample_i}.jsonl", "r") as f: for i, line in enumerate(f): codex_data = json.loads(line) raw_data = prompt_to_data[codex_data["prompt"]] all_codex_data.append((codex_data, raw_data)) except Exception as e: print(e) print(f"{path / output_file_name} not ready yet. skipping.") continue if (path / output_file_name).exists() and not args.overwrite_output_dir: with open(path / output_file_name, "r") as f: line_num = len(f.readlines()) if line_num == len(all_codex_data): print(f"skipping {path / output_file_name}") continue from multiprocessing import Pool if args.num_procs > 1: all_codex_data_with_reverse = [] chunk_size = len(all_codex_data) // args.num_procs + 1 chunked_all_codex_data = [ all_codex_data[chunk_start : chunk_start + chunk_size] for chunk_start in range(0, len(all_codex_data), chunk_size) ] with Pool(processes=args.num_procs) as pool: for codex_data_with_reverse in pool.imap( partial(batch_query_reverse_logp, args=args), chunked_all_codex_data, ): all_codex_data_with_reverse.extend(codex_data_with_reverse) else: all_codex_data_with_reverse = batch_query_reverse_logp( all_codex_data, args ) with open(path / output_file_name, "w") as f: for codex_data_with_reverse in all_codex_data_with_reverse: codex_data_json = json.dumps(codex_data_with_reverse) f.write(codex_data_json + "\n")
coder_reviewer_reranking-main
zeroshot_reviewer.py
# Copyright (c) Meta Platforms, Inc. and affiliates. import argparse import copy import json import openai import os import pickle import random import signal import time from glob import glob from nltk.translate.bleu_score import sentence_bleu from tqdm import tqdm, trange import re codex_name_mapping = { "codex-cushman": "code-cushman-001", "codex002": "code-davinci-002", "codex001": "code-davinci-001", } def codex_greedy(configs, prompt, max_tokens=512): response = openai.Completion.create( engine=codex_name_mapping[configs.engine_name] if configs.engine_name is not None else "davinci-codex", prompt=prompt, temperature=0, max_tokens=max_tokens, top_p=1, frequency_penalty=0, presence_penalty=0, stop=configs.end_template, ) return response["choices"][0]["text"], None, None def codex_sample(configs, prompt, max_tokens=512): response = openai.Completion.create( engine=codex_name_mapping[configs.engine_name] if configs.engine_name is not None else "davinci-codex", prompt=prompt, temperature=configs.temperature, max_tokens=max_tokens, top_p=configs.top_p, frequency_penalty=0, presence_penalty=0, logprobs=1, stop=configs.end_template, ) return ( response["choices"][0]["text"], response["choices"][0]["logprobs"]["tokens"], response["choices"][0]["logprobs"]["token_logprobs"], ) def codex_batch_greedy(configs, batch_prompts, max_tokens=512): raise NotImplementedError def codex_batch_sample(configs, batch_prompts, max_tokens=512): response = openai.Completion.create( engine=codex_name_mapping[configs.engine_name] if configs.engine_name is not None else "davinci-codex", prompt=batch_prompts, temperature=configs.temperature, max_tokens=max_tokens, top_p=configs.top_p, frequency_penalty=0, presence_penalty=0, logprobs=1, stop=configs.end_template, ) return [ ( response["choices"][batch_i]["text"], response["choices"][batch_i]["logprobs"]["tokens"], response["choices"][batch_i]["logprobs"]["token_logprobs"], ) for batch_i in range(len(batch_prompts)) ] def process_batch_examples(args_with_idx): batch_i, batch_args = args_with_idx all_prompts = [] for args in batch_args: src, trg, info, prompt_prefix, configs = args if configs.dataset in ["mbpp_sanitized", "humaneval", "codet_humaneval"]: prompt = src else: prompt = prompt_prefix + configs.example_template.format(src=src, info=info) all_prompts.append(prompt) max_tokens = configs.max_tokens while True: if configs.engine_name == "codex002": openai.organization = os.getenv(f"OPENAI_ORG{(batch_i%3)+1}") else: openai.organization = os.getenv("OPENAI_ORG1") try: batch_results = ( codex_batch_greedy(configs, all_prompts, max_tokens) if configs.mode == "greedy" else codex_batch_sample(configs, all_prompts, max_tokens) ) break except openai.error.InvalidRequestError as e: print(f"Context len: halving gen tokens, curr: {max_tokens}", end="\r") max_tokens = max_tokens // 2 if max_tokens < 32: raise ValueError("Prompt too long") except openai.error.RateLimitError as e: print(type(e), re.search("Current: .+ / min", str(e))[0], end="\r") time.sleep(30) except Exception as e: print(type(e), e) time.sleep(10) all_results = [] for args, prompt, (trg_prediction, tokens, logprobs) in zip( batch_args, all_prompts, batch_results ): src, trg, info, prompt_prefix, configs = args if "humaneval" in configs.dataset or configs.dataset == "mbpp_sanitized": if "\nprint" in trg_prediction: for i in range(0, len(tokens) - 1): if tokens[i : i + 2] == ["\n", "print"]: break tokens = tokens[:i] logprobs = logprobs[:i] trg_prediction = "".join(tokens) if i == len(tokens) - 1: raise ValueError("not matched") result = { "prompt": prompt, "src": src, "trg_prediction": trg_prediction, "reference": trg, "tokens": tokens, "logprobs": logprobs, } all_results.append(json.dumps(result)) return all_results def process_one_example(args): src, trg, info, prompt_prefix, configs = args if configs.dataset in ["mbpp_sanitized", "humaneval", "codet_humaneval"]: prompt = src else: prompt = prompt_prefix + configs.example_template.format(src=src, info=info) max_tokens = configs.max_tokens while True: try: trg_prediction, tokens, logprobs = ( codex_greedy(configs, prompt, max_tokens) if configs.mode == "greedy" else codex_sample(configs, prompt, max_tokens) ) break except openai.error.InvalidRequestError as e: print(f"Context len: halving gen tokens, curr: {max_tokens}", end="\r") max_tokens = max_tokens // 2 if max_tokens < 32: raise ValueError("Prompt too long") except openai.error.RateLimitError as e: print(type(e), re.search("Current: .+ / min", str(e))[0], end="\r") time.sleep(30) except Exception as e: print(type(e), e) time.sleep(10) import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore") try: bleu_score = sentence_bleu( [[ch for ch in trg]], [ch for ch in trg_prediction] ) except: bleu_score = 0 if "humaneval" in configs.dataset or configs.dataset == "mbpp_sanitized": if "\nprint" in trg_prediction: for i in range(0, len(tokens) - 1): if tokens[i : i + 2] == ["\n", "print"]: break tokens = tokens[:i] logprobs = logprobs[:i] trg_prediction = "".join(tokens) if i == len(tokens) - 1: raise ValueError("not matched") return json.dumps( { "prompt": prompt, "src": src, "trg_prediction": trg_prediction, "reference": trg, "tokens": tokens, "logprobs": logprobs, "bleu": bleu_score, } ) def codex_with_info(configs, dataset, prefixes): # model openai.api_key = os.getenv("OPENAI_API_KEY") prompt_prefix = "".join( [ configs.prompt_template.format(src=x[0], trg=x[1], info=x[2]) for x in prefixes ] ) # save folder if configs.top_p == 1: save_dir = f"{configs.output_path}/seed-{configs.seed}/{configs.n_prompts}-shot/{configs.mode}-{configs.temperature}" else: save_dir = f"{configs.output_path}/seed-{configs.seed}/{configs.n_prompts}-shot/{configs.mode}-{configs.temperature}-p{configs.top_p}" if configs.max_tokens != 512: save_dir += f"-max{configs.max_tokens}" os.system(f"mkdir -p {save_dir}") # save configs and prefixes if configs.rank == 0: with open(f"{save_dir}/prefixes.json", "w") as fout: json.dump(prefixes, fout) fout.close() with open(f"{save_dir}/configs.pkl", "wb") as fout: pickle.dump(configs, fout) fout.close() ofname = f"{save_dir}/{configs.split}-{configs.rank}.jsonl" if os.path.exists(ofname): return from multiprocessing import Pool all_args = [] for (src, trg, info) in dataset: all_args.append((src, trg, info, prompt_prefix, configs)) all_jsons = [] if configs.n_procs > 1: all_args = [ all_args[chunk_start : chunk_start + configs.batch_size] for chunk_start in range(0, len(all_args), configs.batch_size) ] with Pool(processes=configs.n_procs) as pool: for result_json in tqdm( pool.imap(process_batch_examples, enumerate(all_args)), total=len(all_args), ): all_jsons.extend(result_json) else: for batch_i, batch_start in enumerate( trange(0, len(all_args), configs.batch_size) ): batch_args = all_args[batch_start : batch_start + configs.batch_size] all_jsons.extend(process_batch_examples((batch_i, batch_args))) with open(ofname, "w") as fout: for jsonf in all_jsons: fout.write(jsonf + "\n") """ example collector: <src, trg, info> """ class CollectorWithInfo(object): def __init__(self, configs, dataset): self.configs = configs self.dataset = dataset def __call__(self, rank, local_rank, world_size): configs = copy.deepcopy(self.configs) configs.rank = rank configs.gpu = local_rank configs.world_size = world_size args = [] for seed in self.configs.seed: for n_prompts in self.configs.n_prompts: args.append((seed, n_prompts, configs.temperature)) for seed, n_prompts, temperature in tqdm(args): configs.n_prompts = n_prompts configs.seed = seed configs.temperature = temperature random.seed(configs.seed) if configs.n_prompts == 0: prefixes = [] else: if configs.saved_prefixes_path_template is not None: prefix_pool = list() for path in glob( configs.saved_prefixes_path_template, recursive=True ): prefix_pool.extend(json.load(open(path))) prefix_pool = sorted(set([tuple(x) for x in prefix_pool])) prefixes = random.sample(prefix_pool, configs.n_prompts) else: prefixes = random.sample( self.dataset.data["train"], configs.n_prompts ) if configs.shuffle_prefix: original_prefixes = copy.deepcopy(prefixes) while original_prefixes == prefixes: random.shuffle(prefixes) codex_with_info(configs, self.dataset.data[configs.split], prefixes) @staticmethod def parse_args(main_parser=None): if main_parser is None: main_parser = argparse.ArgumentParser() subparsers = main_parser.add_subparsers(title="commands", dest="mode") # collect parser = subparsers.add_parser("collect", help="collecting stage") parser.add_argument("--output-path", type=str, required=True) parser.add_argument( "--split", type=str, default="dev", choices=["train", "dev", "test"] ) parser.add_argument("--seed", type=int, nargs="+", default=[0]) parser.add_argument("--n-procs", type=int, default=1) parser.add_argument( "--n-prompts", type=int, nargs="+", default=[3], help="number of few-shot prompt examples", ) parser.add_argument( "--mode", type=str, default="greedy", choices=["greedy", "sample"] ) parser.add_argument( "--batch-size", type=int, default=5, help="number of sampled examples under the sampling mode", ) parser.add_argument( "--max-tokens", type=int, default=512, help="number of sampled examples under the sampling mode", ) parser.add_argument( "--temperature", type=float, default=0.3, help="sample temperature" ) parser.add_argument( "--top_p", type=float, default=1.0, help="sample temperature" ) parser.add_argument( "--prompt-template", type=str, default="<info>{info}</info>\n<text>{src}</text>\n<code>{trg}</code>\n", ) parser.add_argument( "--example-template", type=str, default="<info>{info}</info>\n<text>{src}</text>\n<code>", ) parser.add_argument("--end-template", type=str, default="</code>") parser.add_argument("--shuffle-prefix", action="store_true", default=False) parser.add_argument("--saved-prefixes-path-template", type=str, default=None) parser.add_argument( "--engine-name", type=str, default="codex-cushman", choices=["codex-cushman", "codex001", "codex002"], ) # slurm arguments parser.add_argument("--slurm-ntasks", type=int, default=None) parser.add_argument("--slurm-ngpus", type=int, default=0) parser.add_argument("--slurm-nnodes", type=int, default=1) parser.add_argument("--slurm-partition", type=str, default="devlab") args = main_parser.parse_args() return args @classmethod def from_args(cls, args=None, dataset=None): if args is None: args = cls.parse_args() assert dataset is not None return cls(args, dataset)
coder_reviewer_reranking-main
collectors.py
# Copyright (c) Meta Platforms, Inc. and affiliates. from data import SpiderDataset from collectors import CollectorWithInfo import argparse if __name__ == "__main__": dataset = SpiderDataset() parser = argparse.ArgumentParser() parser.add_argument("--num_seeds", type=int, default=25) parser.add_argument("--num_samples", type=int, default=5) args = CollectorWithInfo.parse_args(parser) args.dataset = "spider" args.seed = list(range(args.num_seeds)) collector = CollectorWithInfo.from_args(args, dataset) for i in range(args.num_samples): collector(i, i, 5)
coder_reviewer_reranking-main
collect_spider.py
# Copyright (c) Meta Platforms, Inc. and affiliates. import bashlex import json import os import pickle import regex import signal import subprocess import tempfile import threading from datasets import load_metric from glob import glob from nltk.translate.bleu_score import sentence_bleu from tqdm import tqdm from dataset.human_eval.human_eval.evaluation import evaluate_functional_correctness import numpy as np from collections import Counter from data import MBPPGoogleDataset, HumanEvalDataset, MBPPSanDataset from utils_sql import * from time import sleep class Command(object): def __init__(self, cmd): self.cmd = cmd self.process = None def run(self, timeout): def target(): self.process = subprocess.Popen(self.cmd, shell=True, preexec_fn=os.setsid) self.process.communicate() thread = threading.Thread(target=target) thread.start() thread.join(timeout) if thread.is_alive(): os.killpg(self.process.pid, signal.SIGTERM) thread.join() return self.process.returncode class PythonFunctionExecutor(object): def __init__(self, function_content, function_call, timeout=10): self.function_content = function_content self.function_content = self.function_content.replace("</code>", "") self.function_call = function_call self.timeout = timeout def __call__(self, i, use_json=False): tempdir = tempfile.TemporaryDirectory() with open(f"{tempdir.name}/code-{i}.py", "w") as fout: print(self.function_content, file=fout) print(f"result = {self.function_call}", file=fout) print(f"import pickle", file=fout) print( f'pickle.dump(result, open("{tempdir.name}/execution_result-{i}.pkl", "wb"))', file=fout, ) command = Command(f"python {tempdir.name}/code-{i}.py >/dev/null 2>&1") execution_status = command.run(timeout=self.timeout) if execution_status == 0: try: execution_results = pickle.load( open(f"{tempdir.name}/execution_result-{i}.pkl", "rb") ) except: execution_results = None else: execution_results = None tempdir.cleanup() return execution_status, execution_results def mbpp_execute_one_assertion(args): data_item, code_item, i = args assertion = data_item[-1] command = regex.match(f"assert (.+)==.+", assertion).group(1) python_function = code_item["trg_prediction"] executor = PythonFunctionExecutor(python_function, command) execution_result = executor(i) return execution_result def mbpp_execute_multiple_assertion(args): data_item, code_item, i = args execution_result = list() python_function = code_item["trg_prediction"] for assertion_i, assertion in enumerate(data_item[-1]): command = regex.match(f"assert (.+)==.+", assertion).group(1) executor = PythonFunctionExecutor(python_function, command) execution_result.append(executor(f"{i}-{assertion_i}")) return execution_result def mbpp_execute_multiple_assertion_pass(args): data_item, code_item, i = args execution_result = list() python_function = code_item["trg_prediction"] for assertion_i, assertion in enumerate(data_item[-1]): command = regex.match(f"assert (.+==.+)", assertion).group(1) executor = PythonFunctionExecutor(python_function, f"({command})") execute_stats, execute_result = executor(f"{i}-{assertion_i}") # if isinstance(execute_result, tuple) and len(execute_result) == 2: # execute_result = execute_result[0] # assert execute_result is None or isinstance(execute_result, bool) execution_result.append((execute_stats, execute_result)) return execution_result from multiprocessing import Pool def execute_mbpp_google_folder(base_path, num_procs=10, verbose=False): # single assertion dataset = MBPPGoogleDataset(mode="assertion") for path in tqdm( glob(f"{base_path}/*jsonl"), leave=False, desc="exec one", disable=not verbose ): # execute first assertion call if "with-reverse" in path: continue if os.path.exists(path.replace("jsonl", "exec.pkl")): continue split = os.path.basename(path).split("-")[0] execution_results = list() all_args = [] for i, line in enumerate(open(path).readlines()): data_item = dataset.data[split][i] code_item = json.loads(line) all_args.append((data_item, code_item, i)) if num_procs > 1: with Pool(processes=num_procs) as pool: for execution_result in pool.imap(mbpp_execute_one_assertion, all_args): execution_results.append(execution_result) else: for execution_result in map(mbpp_execute_one_assertion, all_args): execution_results.append(execution_result) with open(path.replace("jsonl", "exec.pkl"), "wb") as fout: pickle.dump(execution_results, fout) # multiple assertions (cheating) dataset = MBPPGoogleDataset(mode="assertion-full") for path in tqdm( glob(f"{base_path}/*jsonl"), leave=False, desc="exec multiple", disable=not verbose, ): # execute all assertion calls if "with-reverse" in path: continue if os.path.exists(path.replace("jsonl", "execfull.pkl")): continue split = os.path.basename(path).split("-")[0] execution_results = list() all_args = [] for i, line in enumerate(open(path).readlines()): data_item = dataset.data[split][i] code_item = json.loads(line) import uuid all_args.append((data_item, code_item, str(uuid.uuid4()))) if num_procs > 1: with Pool(processes=num_procs) as pool: for execution_result in pool.imap( mbpp_execute_multiple_assertion, all_args ): execution_results.append(execution_result) else: for execution_result in map(mbpp_execute_multiple_assertion, all_args): execution_results.append(execution_result) with open(path.replace("jsonl", "execfull.pkl"), "wb") as fout: pickle.dump(execution_results, fout) # multiple assertions (pass or fail) for path in tqdm( glob(f"{base_path}/*jsonl"), leave=False, desc="exec-multiple-pass", disable=not verbose, ): if "with-reverse" in path: continue if os.path.exists(path.replace("jsonl", "execfullpass.pkl")): continue split = os.path.basename(path).split("-")[0] execution_results = list() all_args = [] for i, line in enumerate(open(path).readlines()): data_item = dataset.data[split][i] code_item = json.loads(line) all_args.append((data_item, code_item, i)) if num_procs > 1: with Pool(processes=num_procs) as pool: for execution_result in pool.imap( mbpp_execute_multiple_assertion_pass, all_args ): execution_results.append(execution_result) else: for execution_result in map(mbpp_execute_multiple_assertion_pass, all_args): execution_results.append(execution_result) # with open(path.replace('jsonl', 'execfullpass.pkl'), 'rb') as fout: # gt_execution_results = pickle.load(fout) # for i, (a, b) in enumerate(zip(execution_results, gt_execution_results)): # if a != b: # print(i, (a, b)) with open(path.replace("jsonl", "execfullpass.pkl"), "wb") as fout: pickle.dump(execution_results, fout) def execute_spider_folder( base_path, db_path="dataset/spider/database", gold_path="dataset/spider", table_path="dataset/spider/tables.json", timeout=10, ): kmaps = build_foreign_key_map_from_json(table_path) for path in glob(f"{base_path}/*jsonl"): if "with-reverse" in path: continue if os.path.exists(path.replace("jsonl", "exec.pkl")): continue execution_results = list() split = os.path.basename(path).split("-")[0] file_gold_path = f"{gold_path}/{split}_gold.sql" with open(file_gold_path) as f: glist = [l.strip().split("\t") for l in f if len(l.strip()) > 0] with open(path) as f: plist = [json.loads(l)["trg_prediction"] for l in f] for p_str, (_, db_name) in tqdm(list(zip(plist, glist))): db = os.path.join(db_path, db_name, db_name + ".sqlite") schema = Schema(get_schema(db)) try: p_sql = get_sql(schema, p_str) except: # If p_sql is not valid, then we will use an empty sql to evaluate with the correct sql p_sql = { "except": None, "from": {"conds": [], "table_units": []}, "groupBy": [], "having": [], "intersect": None, "limit": None, "orderBy": [], "select": [False, []], "union": None, "where": [], } # rebuild sql for value evaluation kmap = kmaps[db_name] p_valid_col_units = build_valid_col_units( p_sql["from"]["table_units"], schema ) p_sql = rebuild_sql_val(p_sql) p_sql = rebuild_sql_col(p_valid_col_units, p_sql, kmap) execution_result = execute(db, p_str, p_sql, timeout) execution_results.append(execution_result) with open(path.replace("jsonl", "exec.pkl"), "wb") as fout: pickle.dump(execution_results, fout) def simulate_bash_exec(command): return list(bashlex.split(command)) def execute_mbpp_google_folder_one(base_path, num_procs=5, verbose=False, tag=""): # single assertion path = str(base_path) dataset = MBPPGoogleDataset(mode="assertion") out_name = "exec.pkl" if not (os.path.exists(path.replace("jsonl", out_name))): split = os.path.basename(path).split("-")[0] execution_results = list() all_args = [] for i, line in enumerate(open(path).readlines()): data_item = dataset.data[split][i] code_item = json.loads(line) all_args.append((data_item, code_item, i)) if num_procs > 1: with Pool(processes=num_procs) as pool: for execution_result in tqdm( pool.imap(mbpp_execute_one_assertion, all_args), total=len(all_args), leave=False, disable=not verbose, desc="exec on", ): execution_results.append(execution_result) else: for execution_result in map( mbpp_execute_one_assertion, tqdm(all_args, disable=not verbose) ): execution_results.append(execution_result) with open(path.replace("jsonl", out_name), "wb") as fout: pickle.dump(execution_results, fout) # mltiple assertions (cheating) dataset = MBPPGoogleDataset(mode="assertion-full") path = str(base_path) out_name = "execfull.pkl" if not (os.path.exists(path.replace("jsonl", out_name))): split = os.path.basename(path).split("-")[0] execution_results = list() all_args = [] for i, line in enumerate(open(path).readlines()): data_item = dataset.data[split][i] code_item = json.loads(line) all_args.append((data_item, code_item, i)) if num_procs > 1: with Pool(processes=num_procs) as pool: for execution_result in tqdm( pool.imap(mbpp_execute_multiple_assertion, all_args), total=len(all_args), leave=False, disable=not verbose, desc="exec all", ): execution_results.append(execution_result) else: for execution_result in map( mbpp_execute_multiple_assertion, tqdm(all_args, disable=not verbose) ): execution_results.append(execution_result) with open(path.replace("jsonl", out_name), "wb") as fout: pickle.dump(execution_results, fout) # mltiple assertions (pass or fail) path = str(base_path) out_name = "execfullpass.pkl" if not (os.path.exists(path.replace("jsonl", out_name))): split = os.path.basename(path).split("-")[0] execution_results = list() all_args = [] for i, line in enumerate(open(path).readlines()): data_item = dataset.data[split][i] code_item = json.loads(line) all_args.append((data_item, code_item, i)) if num_procs > 1: with Pool(processes=num_procs) as pool: for execution_result in tqdm( pool.imap(mbpp_execute_multiple_assertion_pass, all_args), total=len(all_args), leave=False, disable=not verbose, desc="pass or fail", ): execution_results.append(execution_result) else: for execution_result in map( mbpp_execute_multiple_assertion_pass, tqdm(all_args, disable=not verbose), ): execution_results.append(execution_result) with open(path.replace("jsonl", out_name), "wb") as fout: pickle.dump(execution_results, fout) def execute_spider_folder_one( base_path, db_path="dataset/spider/database", gold_path="dataset/spider", table_path="dataset/spider/tables.json", timeout=10, verbose=False, tag="", ): kmaps = build_foreign_key_map_from_json(table_path) path = str(base_path) out_name = "exec.pkl" if tag == "" else f"exec.pkl" if not (os.path.exists(path.replace("jsonl", f"{out_name}"))): execution_results = list() split = os.path.basename(path).split("-")[0] file_gold_path = f"{gold_path}/{split}_gold.sql" with open(file_gold_path) as f: glist = [l.strip().split("\t") for l in f if len(l.strip()) > 0] with open(path) as f: plist = [json.loads(l)["trg_prediction"] for l in f] count = 0 for p_str, (_, db_name) in tqdm( list(zip(plist, glist)), disable=not verbose, desc="SQL exec" ): db = os.path.join(db_path, db_name, db_name + ".sqlite") schema = Schema(get_schema(db)) try: p_sql = get_sql(schema, p_str) except: # If p_sql is not valid, then we will use an empty sql to evaluate with the correct sql p_sql = { "except": None, "from": {"conds": [], "table_units": []}, "groupBy": [], "having": [], "intersect": None, "limit": None, "orderBy": [], "select": [False, []], "union": None, "where": [], } # rebuild sql for value evaluation kmap = kmaps[db_name] p_valid_col_units = build_valid_col_units( p_sql["from"]["table_units"], schema ) p_sql = rebuild_sql_val(p_sql) p_sql = rebuild_sql_col(p_valid_col_units, p_sql, kmap) execution_result = execute(db, p_str, p_sql, timeout) execution_results.append(execution_result) count += 1 with open(path.replace("jsonl", out_name), "wb") as fout: pickle.dump(execution_results, fout) def humaneval_postprocess( completion, ): keep_lines = [] for l in completion.split("\n"): if not l.startswith("print"): keep_lines.append(l) return "\n".join(keep_lines) def humaneval_execute_one_assertion(problem): assertion = problem["assertion"] try: command = regex.match(f"assert (.+)==.+", assertion).group(1) except: command = regex.match(f"assert (.+)", assertion).group(1) python_function = problem["prompt"] + problem["completion"] executor = PythonFunctionExecutor(python_function, command) execution_result = executor(problem["task_id"].split("/")[1]) return execution_result def humaneval_execute_multiple_assertion(problem): execution_result = list() python_function = problem["prompt"] + problem["completion"] task_id = problem["task_id"].split("/")[1] for assertion_i, assertion in enumerate(problem["assertion"]): try: try: command = regex.match(f"assert (.+)==.+", assertion).group(1) except: command = regex.match(f"assert (.+)", assertion).group(1) except: print(problem["assertion"]) print(problem["task_id"]) breakpoint() executor = PythonFunctionExecutor(python_function, command) execution_result.append(executor(f"{task_id}-{assertion_i}")) return execution_result def humaneval_execute_generated_assertion(problem): execution_result = list() python_function = problem["prompt"] + problem["completion"] task_id = problem["task_id"].split("/")[1] total_matched = 0 for assertion_i, assertion in enumerate(problem["gen_assertion"]): matched = False for pattern in ["assert (.+)==.+", "assert (.+) is .+", "assert (.+)"]: try: command = regex.match(pattern, assertion).group(1) matched = True break except: pass if matched: executor = PythonFunctionExecutor(python_function, command) execution_result.append(executor(f"{task_id}-{assertion_i}")) total_matched += int(matched) if total_matched > 20: break return execution_result def execute_humaneval_folder_one( base_path, timeout=10, verbose=False, tag="", num_procs=1, dataset_choice="humaneval", ): path = str(base_path) if dataset_choice in ["humaneval", "codet_humaneval"]: dataset_cls = HumanEvalDataset if dataset_choice == "codet_humaneval": dataset_problem_file = "dataset/human_eval/dataset/CodeTHumanEval.jsonl" assertion_file = "dataset/human_eval/dataset/HumanEval.jsonl" else: dataset_problem_file = "dataset/human_eval/dataset/HumanEval.jsonl" assertion_file = "" elif dataset_choice == "mbpp_sanitized": dataset_problem_file = "dataset/mbpp/mbpp_sanitized_for_code_generation.jsonl" assertion_file = "" dataset_cls = MBPPSanDataset else: raise ValueError("Invalid data choice") dataset = dataset_cls( path=dataset_problem_file, assertion_path=assertion_file, mode="assertion" ) prompt_to_problem = {p["prompt"]: p for task_id, p in dataset.raw_data.items()} out_name = "exec.pkl" problem_with_completions = [] for line in open(path).readlines(): code_item = json.loads(line) problem = prompt_to_problem[code_item["prompt"]] problem["completion"] = humaneval_postprocess(code_item["trg_prediction"]) problem_with_completions.append(problem) if not (os.path.exists(path.replace("jsonl", out_name))): execution_results = [] if num_procs > 1: with Pool(processes=num_procs) as pool: for execution_result in pool.imap( humaneval_execute_one_assertion, problem_with_completions ): execution_results.append(execution_result) else: for execution_result in map( humaneval_execute_one_assertion, problem_with_completions ): execution_results.append(execution_result) with open(path.replace("jsonl", out_name), "wb") as fout: pickle.dump(execution_results, fout) dataset = dataset_cls( path=dataset_problem_file, assertion_path=assertion_file, mode="assertion-all" ) prompt_to_problem = {p["prompt"]: p for task_id, p in dataset.raw_data.items()} problem_with_completions = [] for line in open(path).readlines(): code_item = json.loads(line) problem = prompt_to_problem[code_item["prompt"]] problem["completion"] = humaneval_postprocess(code_item["trg_prediction"]) problem_with_completions.append(problem) out_name = "execfull.pkl" if not (os.path.exists(path.replace("jsonl", out_name))): execution_results = [] if num_procs > 1: with Pool(processes=num_procs) as pool: for execution_result in pool.imap( humaneval_execute_multiple_assertion, problem_with_completions ): execution_results.append(execution_result) else: for execution_result in map( humaneval_execute_multiple_assertion, problem_with_completions ): execution_results.append(execution_result) with open(path.replace("jsonl", out_name), "wb") as fout: pickle.dump(execution_results, fout) out_name = "execfullpass.pkl" if not (os.path.exists(path.replace("jsonl", out_name))): results, pass_at_k, extras = evaluate_functional_correctness( samples=problem_with_completions, sample_file=None, k=[1], problem_file=dataset_problem_file, suppress=True, timeout=timeout, ) all_passed = [] for result in results.values(): result.sort() passed = [r[1]["passed"] for r in result] assert len(passed) == 1 all_passed.append(passed[0]) with open(path.replace("jsonl", out_name), "wb") as fout: pickle.dump(all_passed, fout) else: all_passed = pickle.load(open(path.replace("jsonl", out_name), "rb")) def execute_nl2bash_folder_one( base_path, ): bleu = load_metric("bleu") path = str(base_path) if all( ( os.path.exists(path.replace(".jsonl", ".exec.pkl")), os.path.exists(path.replace(".jsonl", ".exec.splitted.pkl")), os.path.exists(path.replace(".jsonl", ".exec.simulate.pkl")), os.path.exists(path.replace(".jsonl", ".exec.bleu.pkl")), ) ): # return pass all_exec_results = [] all_exec_splitted_results = [] all_simulate_exec = [] all_char_bleu = [] for line in tqdm(open(path).readlines()): code_item = json.loads(line) code_item["trg_prediction"] try: with time_limit(10): bashlex.parse(code_item["trg_prediction"]) all_exec_results.append(True) except: all_exec_results.append(False) try: with time_limit(10): splitted_trg_pred = simulate_bash_exec(code_item["trg_prediction"]) except: splitted_trg_pred = list() simulate_exec = Counter(splitted_trg_pred) all_exec_splitted_results.append(splitted_trg_pred) all_simulate_exec.append(simulate_exec) try: with time_limit(10): all_char_bleu.append( bleu.compute( predictions=[[ch for ch in code_item["reference"]]], references=[[[ch for ch in code_item["trg_prediction"]]]], )["bleu"] ) except: all_char_bleu.append(0) with open(path.replace(".jsonl", ".exec.pkl"), "wb") as fout: pickle.dump(all_exec_results, fout) with open(path.replace(".jsonl", ".exec.splitted.pkl"), "wb") as fout: pickle.dump(all_exec_splitted_results, fout) with open(path.replace(".jsonl", ".exec.simulate.pkl"), "wb") as fout: pickle.dump(all_simulate_exec, fout) with open(path.replace(".jsonl", ".exec.bleu.pkl"), "wb") as fout: pickle.dump(all_char_bleu, fout)
coder_reviewer_reranking-main
execution.py
# Copyright (c) Meta Platforms, Inc. and affiliates. import argparse import data from collectors import CollectorWithInfo if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--info-mode", type=str, default="assertion", choices=["function_name", "assertion"], ) parser.add_argument( "--dataset-type", type=str, default="MBPPGoogleDataset", choices=["MBPPDataset", "MBPPGoogleDataset"], ) parser.add_argument("--num_seeds", type=int, default=25) parser.add_argument("--num_samples", type=int, default=5) args = CollectorWithInfo.parse_args(parser) args.seed = list(range(args.num_seeds)) args.dataset = "mbpp" args.split = "test" dataset = getattr(data, args.dataset_type)(mode=args.info_mode) collector = CollectorWithInfo.from_args(args, dataset) for i in range(args.num_samples): collector(i, i, 5)
coder_reviewer_reranking-main
collect_mbpp.py
# Copyright (c) Meta Platforms, Inc. and affiliates. import shutil import torch from pathlib import Path import os from glob import glob from argparse import ArgumentParser from tqdm import tqdm, trange import torch.distributed as dist from execution import ( execute_humaneval_folder_one, execute_mbpp_google_folder_one, execute_spider_folder_one, execute_nl2bash_folder_one, ) from pathlib import Path parser = ArgumentParser() parser.add_argument("--batch_size", type=int, default=2) parser.add_argument("--dataset", type=str, default="mbpp") parser.add_argument("--tag", type=str, default="") parser.add_argument("--split", type=str, default="test") parser.add_argument("--num_seeds", type=int, default=5) parser.add_argument("--num_samples", type=int, default=5) parser.add_argument("--num_prompts", type=int, default=1) parser.add_argument( "--in_data_path", type=str, default="/private/home/tianyizzz/projects/mbr-exec-data/mbr-exec-codex001/", ) parser.add_argument("--temperature", type=float, default=0.3) parser.add_argument("--max_tokens", type=int, default=512) parser.add_argument("--top_p", type=float, default=1.0) parser.add_argument("--rank", type=int, default=0) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument("--world_size", type=int, default=1) args = parser.parse_args() args.rank = int(os.environ.get("LOCAL_RANK", 0)) # if args.world_size > 1: # dist.init_process_group("gloo", rank=args.rank, world_size=args.world_size) paths = [] if args.temperature > 0: for seed in range(args.num_seeds): for i in range(args.num_samples): if (seed * args.num_samples + i) % args.world_size == args.rank: out_dir = f"sample-{args.temperature}" if args.top_p != 1.0: out_dir += f"-p{args.top_p}" if args.max_tokens != 512: out_dir += f"-max{args.max_tokens}" if args.tag == "": result_file = f"{args.split}-{i}.jsonl" else: result_file = f"{args.split}-{i}-{args.tag}.jsonl" path = ( Path(args.in_data_path) / args.dataset / f"seed-{seed}" / f"{args.num_prompts}-shot" / out_dir / result_file ) paths.append(path) else: for seed in range(args.num_seeds): i = 0 if (seed * 5 + i) % args.world_size == args.rank: out_dir = f"sample-{args.temperature}" if args.max_tokens != 512: out_dir += f"-max{args.max_tokens}" if args.tag == "": result_file = f"{args.split}-{i}.jsonl" else: result_file = f"{args.split}-{i}-{args.tag}.jsonl" paths.append( Path(args.in_data_path) / args.dataset / f"seed-{seed}" / f"{args.num_prompts}-shot" / out_dir / result_file ) for path in tqdm(paths, disable=not args.rank == 0): if args.dataset == "mbpp": execute_mbpp_google_folder_one(path, verbose=args.rank == 0, tag=args.tag) elif args.dataset == "spider": execute_spider_folder_one(path, verbose=args.rank == 0, tag=args.tag) elif "humaneval" in args.dataset or args.dataset == "mbpp_sanitized": execute_humaneval_folder_one( path, verbose=args.rank == 0, tag=args.tag, dataset_choice=args.dataset ) elif args.dataset == "nl2bash": execute_nl2bash_folder_one(path) else: raise ValueError("invalid dataset")
coder_reviewer_reranking-main
multi_exec.py
# Copyright (c) Meta Platforms, Inc. and affiliates. from tqdm import tqdm import os import sqlite3 import pickle as pkl # CONSTANT db_dir = "./dataset/spider/database/" # preloading spider data to reduce io from dataset.spider_official.evaluation import ( build_foreign_key_map_from_json, build_valid_col_units, rebuild_sql_val, rebuild_sql_col, ) from dataset.spider_official.process_sql import ( get_schema, Schema, get_sql, ) kmaps = build_foreign_key_map_from_json("./dataset/spider/tables.json") with open("dataset/spider/dev_gold.sql") as f: glist = [l.strip().split("\t") for l in f.readlines() if len(l.strip()) > 0] all_g_res = [] for gold_sql in tqdm(glist, total=len(glist)): g_str, db = gold_sql db_name = db db = os.path.join(db_dir, db, db + ".sqlite") schema = Schema(get_schema(db)) g_sql = get_sql(schema, g_str) kmap = kmaps[db_name] g_valid_col_units = build_valid_col_units(g_sql["from"]["table_units"], schema) g_sql = rebuild_sql_val(g_sql) g_sql = rebuild_sql_col(g_valid_col_units, g_sql, kmap) conn = sqlite3.connect(f"file:{db}?mode=ro", uri=True) cursor = conn.cursor() # there are potential utf-8 errors try: cursor.execute(g_str) g_res = cursor.fetchall() except: g_res = [] def res_map(res, val_units): rmap = {} for idx, val_unit in enumerate(val_units): key = ( tuple(val_unit[1]) if not val_unit[2] else (val_unit[0], tuple(val_unit[1]), tuple(val_unit[2])) ) rmap[key] = [r[idx] for r in res] return rmap g_val_units = [unit[1] for unit in g_sql["select"][1]] g_res = res_map(g_res, g_val_units) all_g_res.append(g_res) pkl.dump( all_g_res, open( "./dataset/spider/cached_gold_results.pkl", "wb", ), )
coder_reviewer_reranking-main
exec_spider_gold.py
# Copyright (c) Meta Platforms, Inc. and affiliates. ################################ # Assumptions: # 1. sql is correct # 2. only table name has alias # 3. only one intersect/union/except # # val: number(float)/string(str)/sql(dict) # col_unit: (agg_id, col_id, isDistinct(bool)) # val_unit: (unit_op, col_unit1, col_unit2) # table_unit: (table_type, col_unit/sql) # cond_unit: (not_op, op_id, val_unit, val1, val2) # condition: [cond_unit1, 'and'/'or', cond_unit2, ...] # sql { # 'select': (isDistinct(bool), [(agg_id, val_unit), (agg_id, val_unit), ...]) # 'from': {'table_units': [table_unit1, table_unit2, ...], 'conds': condition} # 'where': condition # 'groupBy': [col_unit1, col_unit2, ...] # 'orderBy': ('asc'/'desc', [val_unit1, val_unit2, ...]) # 'having': condition # 'limit': None/limit value # 'intersect': None/sql # 'except': None/sql # 'union': None/sql # } ################################ import json import sqlite3 from nltk import word_tokenize CLAUSE_KEYWORDS = ( "select", "from", "where", "group", "order", "limit", "intersect", "union", "except", ) JOIN_KEYWORDS = ("join", "on", "as") WHERE_OPS = ( "not", "between", "=", ">", "<", ">=", "<=", "!=", "in", "like", "is", "exists", ) UNIT_OPS = ("none", "-", "+", "*", "/") AGG_OPS = ("none", "max", "min", "count", "sum", "avg") TABLE_TYPE = { "sql": "sql", "table_unit": "table_unit", } COND_OPS = ("and", "or") SQL_OPS = ("intersect", "union", "except") ORDER_OPS = ("desc", "asc") class Schema: """ Simple schema which maps table&column to a unique identifier """ def __init__(self, schema): self._schema = schema self._idMap = self._map(self._schema) @property def schema(self): return self._schema @property def idMap(self): return self._idMap def _map(self, schema): idMap = {"*": "__all__"} id = 1 for key, vals in schema.items(): for val in vals: idMap[key.lower() + "." + val.lower()] = ( "__" + key.lower() + "." + val.lower() + "__" ) id += 1 for key in schema: idMap[key.lower()] = "__" + key.lower() + "__" id += 1 return idMap def get_schema(db): """ Get database's schema, which is a dict with table name as key and list of column names as value :param db: database path :return: schema dict """ schema = {} conn = sqlite3.connect(db) cursor = conn.cursor() # fetch table names cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") tables = [str(table[0].lower()) for table in cursor.fetchall()] # fetch table info for table in tables: cursor.execute("PRAGMA table_info({})".format(table)) schema[table] = [str(col[1].lower()) for col in cursor.fetchall()] return schema def get_schema_from_json(fpath): with open(fpath) as f: data = json.load(f) schema = {} for entry in data: table = str(entry["table"].lower()) cols = [str(col["column_name"].lower()) for col in entry["col_data"]] schema[table] = cols return schema def tokenize(string): string = str(string) string = string.replace( "'", '"' ) # ensures all string values wrapped by "" problem?? quote_idxs = [idx for idx, char in enumerate(string) if char == '"'] assert len(quote_idxs) % 2 == 0, "Unexpected quote" # keep string value as token vals = {} for i in range(len(quote_idxs) - 1, -1, -2): qidx1 = quote_idxs[i - 1] qidx2 = quote_idxs[i] val = string[qidx1 : qidx2 + 1] key = "__val_{}_{}__".format(qidx1, qidx2) string = string[:qidx1] + key + string[qidx2 + 1 :] vals[key] = val toks = [word.lower() for word in word_tokenize(string)] # replace with string value token for i in range(len(toks)): if toks[i] in vals: toks[i] = vals[toks[i]] # find if there exists !=, >=, <= eq_idxs = [idx for idx, tok in enumerate(toks) if tok == "="] eq_idxs.reverse() prefix = ("!", ">", "<") for eq_idx in eq_idxs: pre_tok = toks[eq_idx - 1] if pre_tok in prefix: toks = toks[: eq_idx - 1] + [pre_tok + "="] + toks[eq_idx + 1 :] return toks def scan_alias(toks): """Scan the index of 'as' and build the map for all alias""" as_idxs = [idx for idx, tok in enumerate(toks) if tok == "as"] alias = {} for idx in as_idxs: alias[toks[idx + 1]] = toks[idx - 1] return alias def get_tables_with_alias(schema, toks): tables = scan_alias(toks) for key in schema: assert key not in tables, "Alias {} has the same name in table".format(key) tables[key] = key return tables def parse_col(toks, start_idx, tables_with_alias, schema, default_tables=None): """ :returns next idx, column id """ tok = toks[start_idx] if tok == "*": return start_idx + 1, schema.idMap[tok] if "." in tok: # if token is a composite alias, col = tok.split(".") key = tables_with_alias[alias] + "." + col return start_idx + 1, schema.idMap[key] assert ( default_tables is not None and len(default_tables) > 0 ), "Default tables should not be None or empty" for alias in default_tables: table = tables_with_alias[alias] if tok in schema.schema[table]: key = table + "." + tok return start_idx + 1, schema.idMap[key] assert False, "Error col: {}".format(tok) def parse_col_unit(toks, start_idx, tables_with_alias, schema, default_tables=None): """ :returns next idx, (agg_op id, col_id) """ idx = start_idx len_ = len(toks) isBlock = False isDistinct = False if toks[idx] == "(": isBlock = True idx += 1 if toks[idx] in AGG_OPS: agg_id = AGG_OPS.index(toks[idx]) idx += 1 assert idx < len_ and toks[idx] == "(" idx += 1 if toks[idx] == "distinct": idx += 1 isDistinct = True idx, col_id = parse_col(toks, idx, tables_with_alias, schema, default_tables) assert idx < len_ and toks[idx] == ")" idx += 1 return idx, (agg_id, col_id, isDistinct) if toks[idx] == "distinct": idx += 1 isDistinct = True agg_id = AGG_OPS.index("none") idx, col_id = parse_col(toks, idx, tables_with_alias, schema, default_tables) if isBlock: assert toks[idx] == ")" idx += 1 # skip ')' return idx, (agg_id, col_id, isDistinct) def parse_val_unit(toks, start_idx, tables_with_alias, schema, default_tables=None): idx = start_idx len_ = len(toks) isBlock = False if toks[idx] == "(": isBlock = True idx += 1 col_unit1 = None col_unit2 = None unit_op = UNIT_OPS.index("none") idx, col_unit1 = parse_col_unit( toks, idx, tables_with_alias, schema, default_tables ) if idx < len_ and toks[idx] in UNIT_OPS: unit_op = UNIT_OPS.index(toks[idx]) idx += 1 idx, col_unit2 = parse_col_unit( toks, idx, tables_with_alias, schema, default_tables ) if isBlock: assert toks[idx] == ")" idx += 1 # skip ')' return idx, (unit_op, col_unit1, col_unit2) def parse_table_unit(toks, start_idx, tables_with_alias, schema): """ :returns next idx, table id, table name """ idx = start_idx len_ = len(toks) key = tables_with_alias[toks[idx]] if idx + 1 < len_ and toks[idx + 1] == "as": idx += 3 else: idx += 1 return idx, schema.idMap[key], key def parse_value(toks, start_idx, tables_with_alias, schema, default_tables=None): idx = start_idx len_ = len(toks) isBlock = False if toks[idx] == "(": isBlock = True idx += 1 if toks[idx] == "select": idx, val = parse_sql(toks, idx, tables_with_alias, schema) elif '"' in toks[idx]: # token is a string value val = toks[idx] idx += 1 else: try: val = float(toks[idx]) idx += 1 except: end_idx = idx while ( end_idx < len_ and toks[end_idx] != "," and toks[end_idx] != ")" and toks[end_idx] != "and" and toks[end_idx] not in CLAUSE_KEYWORDS and toks[end_idx] not in JOIN_KEYWORDS ): end_idx += 1 idx, val = parse_col_unit( toks[start_idx:end_idx], 0, tables_with_alias, schema, default_tables ) idx = end_idx if isBlock: assert toks[idx] == ")" idx += 1 return idx, val def parse_condition(toks, start_idx, tables_with_alias, schema, default_tables=None): idx = start_idx len_ = len(toks) conds = [] while idx < len_: idx, val_unit = parse_val_unit( toks, idx, tables_with_alias, schema, default_tables ) not_op = False if toks[idx] == "not": not_op = True idx += 1 assert ( idx < len_ and toks[idx] in WHERE_OPS ), "Error condition: idx: {}, tok: {}".format(idx, toks[idx]) op_id = WHERE_OPS.index(toks[idx]) idx += 1 val1 = val2 = None if op_id == WHERE_OPS.index( "between" ): # between..and... special case: dual values idx, val1 = parse_value( toks, idx, tables_with_alias, schema, default_tables ) assert toks[idx] == "and" idx += 1 idx, val2 = parse_value( toks, idx, tables_with_alias, schema, default_tables ) else: # normal case: single value idx, val1 = parse_value( toks, idx, tables_with_alias, schema, default_tables ) val2 = None conds.append((not_op, op_id, val_unit, val1, val2)) if idx < len_ and ( toks[idx] in CLAUSE_KEYWORDS or toks[idx] in (")", ";") or toks[idx] in JOIN_KEYWORDS ): break if idx < len_ and toks[idx] in COND_OPS: conds.append(toks[idx]) idx += 1 # skip and/or return idx, conds def parse_select(toks, start_idx, tables_with_alias, schema, default_tables=None): idx = start_idx len_ = len(toks) assert toks[idx] == "select", "'select' not found" idx += 1 isDistinct = False if idx < len_ and toks[idx] == "distinct": idx += 1 isDistinct = True val_units = [] while idx < len_ and toks[idx] not in CLAUSE_KEYWORDS: agg_id = AGG_OPS.index("none") if toks[idx] in AGG_OPS: agg_id = AGG_OPS.index(toks[idx]) idx += 1 idx, val_unit = parse_val_unit( toks, idx, tables_with_alias, schema, default_tables ) val_units.append((agg_id, val_unit)) if idx < len_ and toks[idx] == ",": idx += 1 # skip ',' return idx, (isDistinct, val_units) def parse_from(toks, start_idx, tables_with_alias, schema): """ Assume in the from clause, all table units are combined with join """ assert "from" in toks[start_idx:], "'from' not found" len_ = len(toks) idx = toks.index("from", start_idx) + 1 default_tables = [] table_units = [] conds = [] while idx < len_: isBlock = False if toks[idx] == "(": isBlock = True idx += 1 if toks[idx] == "select": idx, sql = parse_sql(toks, idx, tables_with_alias, schema) table_units.append((TABLE_TYPE["sql"], sql)) else: if idx < len_ and toks[idx] == "join": idx += 1 # skip join idx, table_unit, table_name = parse_table_unit( toks, idx, tables_with_alias, schema ) table_units.append((TABLE_TYPE["table_unit"], table_unit)) default_tables.append(table_name) if idx < len_ and toks[idx] == "on": idx += 1 # skip on idx, this_conds = parse_condition( toks, idx, tables_with_alias, schema, default_tables ) if len(conds) > 0: conds.append("and") conds.extend(this_conds) if isBlock: assert toks[idx] == ")" idx += 1 if idx < len_ and (toks[idx] in CLAUSE_KEYWORDS or toks[idx] in (")", ";")): break return idx, table_units, conds, default_tables def parse_where(toks, start_idx, tables_with_alias, schema, default_tables): idx = start_idx len_ = len(toks) if idx >= len_ or toks[idx] != "where": return idx, [] idx += 1 idx, conds = parse_condition(toks, idx, tables_with_alias, schema, default_tables) return idx, conds def parse_group_by(toks, start_idx, tables_with_alias, schema, default_tables): idx = start_idx len_ = len(toks) col_units = [] if idx >= len_ or toks[idx] != "group": return idx, col_units idx += 1 assert toks[idx] == "by" idx += 1 while idx < len_ and not (toks[idx] in CLAUSE_KEYWORDS or toks[idx] in (")", ";")): idx, col_unit = parse_col_unit( toks, idx, tables_with_alias, schema, default_tables ) col_units.append(col_unit) if idx < len_ and toks[idx] == ",": idx += 1 # skip ',' else: break return idx, col_units def parse_order_by(toks, start_idx, tables_with_alias, schema, default_tables): idx = start_idx len_ = len(toks) val_units = [] order_type = "asc" # default type is 'asc' if idx >= len_ or toks[idx] != "order": return idx, val_units idx += 1 assert toks[idx] == "by" idx += 1 while idx < len_ and not (toks[idx] in CLAUSE_KEYWORDS or toks[idx] in (")", ";")): idx, val_unit = parse_val_unit( toks, idx, tables_with_alias, schema, default_tables ) val_units.append(val_unit) if idx < len_ and toks[idx] in ORDER_OPS: order_type = toks[idx] idx += 1 if idx < len_ and toks[idx] == ",": idx += 1 # skip ',' else: break return idx, (order_type, val_units) def parse_having(toks, start_idx, tables_with_alias, schema, default_tables): idx = start_idx len_ = len(toks) if idx >= len_ or toks[idx] != "having": return idx, [] idx += 1 idx, conds = parse_condition(toks, idx, tables_with_alias, schema, default_tables) return idx, conds def parse_limit(toks, start_idx): idx = start_idx len_ = len(toks) if idx < len_ and toks[idx] == "limit": idx += 2 return idx, int(toks[idx - 1]) return idx, None def parse_sql(toks, start_idx, tables_with_alias, schema): isBlock = False # indicate whether this is a block of sql/sub-sql len_ = len(toks) idx = start_idx sql = {} if toks[idx] == "(": isBlock = True idx += 1 # parse from clause in order to get default tables from_end_idx, table_units, conds, default_tables = parse_from( toks, start_idx, tables_with_alias, schema ) sql["from"] = {"table_units": table_units, "conds": conds} # select clause _, select_col_units = parse_select( toks, idx, tables_with_alias, schema, default_tables ) idx = from_end_idx sql["select"] = select_col_units # where clause idx, where_conds = parse_where(toks, idx, tables_with_alias, schema, default_tables) sql["where"] = where_conds # group by clause idx, group_col_units = parse_group_by( toks, idx, tables_with_alias, schema, default_tables ) sql["groupBy"] = group_col_units # having clause idx, having_conds = parse_having( toks, idx, tables_with_alias, schema, default_tables ) sql["having"] = having_conds # order by clause idx, order_col_units = parse_order_by( toks, idx, tables_with_alias, schema, default_tables ) sql["orderBy"] = order_col_units # limit clause idx, limit_val = parse_limit(toks, idx) sql["limit"] = limit_val idx = skip_semicolon(toks, idx) if isBlock: assert toks[idx] == ")" idx += 1 # skip ')' idx = skip_semicolon(toks, idx) # intersect/union/except clause for op in SQL_OPS: # initialize IUE sql[op] = None if idx < len_ and toks[idx] in SQL_OPS: sql_op = toks[idx] idx += 1 idx, IUE_sql = parse_sql(toks, idx, tables_with_alias, schema) sql[sql_op] = IUE_sql return idx, sql def load_data(fpath): with open(fpath) as f: data = json.load(f) return data def get_sql(schema, query): toks = tokenize(query) tables_with_alias = get_tables_with_alias(schema.schema, toks) _, sql = parse_sql(toks, 0, tables_with_alias, schema) return sql def skip_semicolon(toks, start_idx): idx = start_idx while idx < len(toks) and toks[idx] == ";": idx += 1 return idx
coder_reviewer_reranking-main
process_sql.py
# Copyright (c) Meta Platforms, Inc. and affiliates. ################################ # val: number(float)/string(str)/sql(dict) # col_unit: (agg_id, col_id, isDistinct(bool)) # val_unit: (unit_op, col_unit1, col_unit2) # table_unit: (table_type, col_unit/sql) # cond_unit: (not_op, op_id, val_unit, val1, val2) # condition: [cond_unit1, 'and'/'or', cond_unit2, ...] # sql { # 'select': (isDistinct(bool), [(agg_id, val_unit), (agg_id, val_unit), ...]) # 'from': {'table_units': [table_unit1, table_unit2, ...], 'conds': condition} # 'where': condition # 'groupBy': [col_unit1, col_unit2, ...] # 'orderBy': ('asc'/'desc', [val_unit1, val_unit2, ...]) # 'having': condition # 'limit': None/limit value # 'intersect': None/sql # 'except': None/sql # 'union': None/sql # } ################################ from __future__ import print_function import os import json import sqlite3 import signal from contextlib import contextmanager import argparse from process_sql import get_schema, Schema, get_sql import sys from time import sleep # Flag to disable value evaluation DISABLE_VALUE = True # Flag to disable distinct in select evaluation DISABLE_DISTINCT = True CLAUSE_KEYWORDS = ( "select", "from", "where", "group", "order", "limit", "intersect", "union", "except", ) JOIN_KEYWORDS = ("join", "on", "as") WHERE_OPS = ( "not", "between", "=", ">", "<", ">=", "<=", "!=", "in", "like", "is", "exists", ) UNIT_OPS = ("none", "-", "+", "*", "/") AGG_OPS = ("none", "max", "min", "count", "sum", "avg") TABLE_TYPE = { "sql": "sql", "table_unit": "table_unit", } COND_OPS = ("and", "or") SQL_OPS = ("intersect", "union", "except") ORDER_OPS = ("desc", "asc") HARDNESS = { "component1": ("where", "group", "order", "limit", "join", "or", "like"), "component2": ("except", "union", "intersect"), } class TimeoutException(Exception): pass @contextmanager def time_limit(seconds: float): def signal_handler(signum, frame): raise TimeoutException("Timed out!") signal.setitimer(signal.ITIMER_REAL, seconds) signal.signal(signal.SIGALRM, signal_handler) try: yield finally: signal.setitimer(signal.ITIMER_REAL, 0) def condition_has_or(conds): return "or" in conds[1::2] def condition_has_like(conds): return WHERE_OPS.index("like") in [cond_unit[1] for cond_unit in conds[::2]] def condition_has_sql(conds): for cond_unit in conds[::2]: val1, val2 = cond_unit[3], cond_unit[4] if val1 is not None and type(val1) is dict: return True if val2 is not None and type(val2) is dict: return True return False def val_has_op(val_unit): return val_unit[0] != UNIT_OPS.index("none") def has_agg(unit): return unit[0] != AGG_OPS.index("none") def accuracy(count, total): if count == total: return 1 return 0 def recall(count, total): if count == total: return 1 return 0 def F1(acc, rec): if (acc + rec) == 0: return 0 return (2.0 * acc * rec) / (acc + rec) def get_scores(count, pred_total, label_total): if pred_total != label_total: return 0, 0, 0 elif count == pred_total: return 1, 1, 1 return 0, 0, 0 def eval_sel(pred, label): pred_sel = pred["select"][1] label_sel = label["select"][1] label_wo_agg = [unit[1] for unit in label_sel] pred_total = len(pred_sel) label_total = len(label_sel) cnt = 0 cnt_wo_agg = 0 for unit in pred_sel: if unit in label_sel: cnt += 1 label_sel.remove(unit) if unit[1] in label_wo_agg: cnt_wo_agg += 1 label_wo_agg.remove(unit[1]) return label_total, pred_total, cnt, cnt_wo_agg def eval_where(pred, label): pred_conds = [unit for unit in pred["where"][::2]] label_conds = [unit for unit in label["where"][::2]] label_wo_agg = [unit[2] for unit in label_conds] pred_total = len(pred_conds) label_total = len(label_conds) cnt = 0 cnt_wo_agg = 0 for unit in pred_conds: if unit in label_conds: cnt += 1 label_conds.remove(unit) if unit[2] in label_wo_agg: cnt_wo_agg += 1 label_wo_agg.remove(unit[2]) return label_total, pred_total, cnt, cnt_wo_agg def eval_group(pred, label): pred_cols = [unit[1] for unit in pred["groupBy"]] label_cols = [unit[1] for unit in label["groupBy"]] pred_total = len(pred_cols) label_total = len(label_cols) cnt = 0 pred_cols = [pred.split(".")[1] if "." in pred else pred for pred in pred_cols] label_cols = [ label.split(".")[1] if "." in label else label for label in label_cols ] for col in pred_cols: if col in label_cols: cnt += 1 label_cols.remove(col) return label_total, pred_total, cnt def eval_having(pred, label): pred_total = label_total = cnt = 0 if len(pred["groupBy"]) > 0: pred_total = 1 if len(label["groupBy"]) > 0: label_total = 1 pred_cols = [unit[1] for unit in pred["groupBy"]] label_cols = [unit[1] for unit in label["groupBy"]] if ( pred_total == label_total == 1 and pred_cols == label_cols and pred["having"] == label["having"] ): cnt = 1 return label_total, pred_total, cnt def eval_order(pred, label): pred_total = label_total = cnt = 0 if len(pred["orderBy"]) > 0: pred_total = 1 if len(label["orderBy"]) > 0: label_total = 1 if ( len(label["orderBy"]) > 0 and pred["orderBy"] == label["orderBy"] and ( (pred["limit"] is None and label["limit"] is None) or (pred["limit"] is not None and label["limit"] is not None) ) ): cnt = 1 return label_total, pred_total, cnt def eval_and_or(pred, label): pred_ao = pred["where"][1::2] label_ao = label["where"][1::2] pred_ao = set(pred_ao) label_ao = set(label_ao) if pred_ao == label_ao: return 1, 1, 1 return len(pred_ao), len(label_ao), 0 def get_nestedSQL(sql): nested = [] for cond_unit in sql["from"]["conds"][::2] + sql["where"][::2] + sql["having"][::2]: if type(cond_unit[3]) is dict: nested.append(cond_unit[3]) if type(cond_unit[4]) is dict: nested.append(cond_unit[4]) if sql["intersect"] is not None: nested.append(sql["intersect"]) if sql["except"] is not None: nested.append(sql["except"]) if sql["union"] is not None: nested.append(sql["union"]) return nested def eval_nested(pred, label): label_total = 0 pred_total = 0 cnt = 0 if pred is not None: pred_total += 1 if label is not None: label_total += 1 if pred is not None and label is not None: cnt += Evaluator().eval_exact_match(pred, label) return label_total, pred_total, cnt def eval_IUEN(pred, label): lt1, pt1, cnt1 = eval_nested(pred["intersect"], label["intersect"]) lt2, pt2, cnt2 = eval_nested(pred["except"], label["except"]) lt3, pt3, cnt3 = eval_nested(pred["union"], label["union"]) label_total = lt1 + lt2 + lt3 pred_total = pt1 + pt2 + pt3 cnt = cnt1 + cnt2 + cnt3 return label_total, pred_total, cnt def get_keywords(sql): res = set() if len(sql["where"]) > 0: res.add("where") if len(sql["groupBy"]) > 0: res.add("group") if len(sql["having"]) > 0: res.add("having") if len(sql["orderBy"]) > 0: res.add(sql["orderBy"][0]) res.add("order") if sql["limit"] is not None: res.add("limit") if sql["except"] is not None: res.add("except") if sql["union"] is not None: res.add("union") if sql["intersect"] is not None: res.add("intersect") # or keyword ao = sql["from"]["conds"][1::2] + sql["where"][1::2] + sql["having"][1::2] if len([token for token in ao if token == "or"]) > 0: res.add("or") cond_units = sql["from"]["conds"][::2] + sql["where"][::2] + sql["having"][::2] # not keyword if len([cond_unit for cond_unit in cond_units if cond_unit[0]]) > 0: res.add("not") # in keyword if ( len( [ cond_unit for cond_unit in cond_units if cond_unit[1] == WHERE_OPS.index("in") ] ) > 0 ): res.add("in") # like keyword if ( len( [ cond_unit for cond_unit in cond_units if cond_unit[1] == WHERE_OPS.index("like") ] ) > 0 ): res.add("like") return res def eval_keywords(pred, label): pred_keywords = get_keywords(pred) label_keywords = get_keywords(label) pred_total = len(pred_keywords) label_total = len(label_keywords) cnt = 0 for k in pred_keywords: if k in label_keywords: cnt += 1 return label_total, pred_total, cnt def count_agg(units): return len([unit for unit in units if has_agg(unit)]) def count_component1(sql): count = 0 if len(sql["where"]) > 0: count += 1 if len(sql["groupBy"]) > 0: count += 1 if len(sql["orderBy"]) > 0: count += 1 if sql["limit"] is not None: count += 1 if len(sql["from"]["table_units"]) > 0: # JOIN count += len(sql["from"]["table_units"]) - 1 ao = sql["from"]["conds"][1::2] + sql["where"][1::2] + sql["having"][1::2] count += len([token for token in ao if token == "or"]) cond_units = sql["from"]["conds"][::2] + sql["where"][::2] + sql["having"][::2] count += len( [ cond_unit for cond_unit in cond_units if cond_unit[1] == WHERE_OPS.index("like") ] ) return count def count_component2(sql): nested = get_nestedSQL(sql) return len(nested) def count_others(sql): count = 0 # number of aggregation agg_count = count_agg(sql["select"][1]) agg_count += count_agg(sql["where"][::2]) agg_count += count_agg(sql["groupBy"]) if len(sql["orderBy"]) > 0: agg_count += count_agg( [unit[1] for unit in sql["orderBy"][1] if unit[1]] + [unit[2] for unit in sql["orderBy"][1] if unit[2]] ) agg_count += count_agg(sql["having"]) if agg_count > 1: count += 1 # number of select columns if len(sql["select"][1]) > 1: count += 1 # number of where conditions if len(sql["where"]) > 1: count += 1 # number of group by clauses if len(sql["groupBy"]) > 1: count += 1 return count class Evaluator: """A simple evaluator""" def __init__(self): self.partial_scores = None def eval_hardness(self, sql): count_comp1_ = count_component1(sql) count_comp2_ = count_component2(sql) count_others_ = count_others(sql) if count_comp1_ <= 1 and count_others_ == 0 and count_comp2_ == 0: return "easy" elif (count_others_ <= 2 and count_comp1_ <= 1 and count_comp2_ == 0) or ( count_comp1_ <= 2 and count_others_ < 2 and count_comp2_ == 0 ): return "medium" elif ( (count_others_ > 2 and count_comp1_ <= 2 and count_comp2_ == 0) or (2 < count_comp1_ <= 3 and count_others_ <= 2 and count_comp2_ == 0) or (count_comp1_ <= 1 and count_others_ == 0 and count_comp2_ <= 1) ): return "hard" else: return "extra" def eval_exact_match(self, pred, label): partial_scores = self.eval_partial_match(pred, label) self.partial_scores = partial_scores for _, score in partial_scores.items(): if score["f1"] != 1: return 0 if len(label["from"]["table_units"]) > 0: label_tables = sorted(label["from"]["table_units"]) pred_tables = sorted(pred["from"]["table_units"]) return label_tables == pred_tables return 1 def eval_partial_match(self, pred, label): res = {} label_total, pred_total, cnt, cnt_wo_agg = eval_sel(pred, label) acc, rec, f1 = get_scores(cnt, pred_total, label_total) res["select"] = { "acc": acc, "rec": rec, "f1": f1, "label_total": label_total, "pred_total": pred_total, } acc, rec, f1 = get_scores(cnt_wo_agg, pred_total, label_total) res["select(no AGG)"] = { "acc": acc, "rec": rec, "f1": f1, "label_total": label_total, "pred_total": pred_total, } label_total, pred_total, cnt, cnt_wo_agg = eval_where(pred, label) acc, rec, f1 = get_scores(cnt, pred_total, label_total) res["where"] = { "acc": acc, "rec": rec, "f1": f1, "label_total": label_total, "pred_total": pred_total, } acc, rec, f1 = get_scores(cnt_wo_agg, pred_total, label_total) res["where(no OP)"] = { "acc": acc, "rec": rec, "f1": f1, "label_total": label_total, "pred_total": pred_total, } label_total, pred_total, cnt = eval_group(pred, label) acc, rec, f1 = get_scores(cnt, pred_total, label_total) res["group(no Having)"] = { "acc": acc, "rec": rec, "f1": f1, "label_total": label_total, "pred_total": pred_total, } label_total, pred_total, cnt = eval_having(pred, label) acc, rec, f1 = get_scores(cnt, pred_total, label_total) res["group"] = { "acc": acc, "rec": rec, "f1": f1, "label_total": label_total, "pred_total": pred_total, } label_total, pred_total, cnt = eval_order(pred, label) acc, rec, f1 = get_scores(cnt, pred_total, label_total) res["order"] = { "acc": acc, "rec": rec, "f1": f1, "label_total": label_total, "pred_total": pred_total, } label_total, pred_total, cnt = eval_and_or(pred, label) acc, rec, f1 = get_scores(cnt, pred_total, label_total) res["and/or"] = { "acc": acc, "rec": rec, "f1": f1, "label_total": label_total, "pred_total": pred_total, } label_total, pred_total, cnt = eval_IUEN(pred, label) acc, rec, f1 = get_scores(cnt, pred_total, label_total) res["IUEN"] = { "acc": acc, "rec": rec, "f1": f1, "label_total": label_total, "pred_total": pred_total, } label_total, pred_total, cnt = eval_keywords(pred, label) acc, rec, f1 = get_scores(cnt, pred_total, label_total) res["keywords"] = { "acc": acc, "rec": rec, "f1": f1, "label_total": label_total, "pred_total": pred_total, } return res def isValidSQL(sql, db): conn = sqlite3.connect(db) cursor = conn.cursor() try: cursor.execute(sql) except: return False return True def print_scores(scores, etype): levels = ["easy", "medium", "hard", "extra", "all"] partial_types = [ "select", "select(no AGG)", "where", "where(no OP)", "group(no Having)", "group", "order", "and/or", "IUEN", "keywords", ] print("{:20} {:20} {:20} {:20} {:20} {:20}".format("", *levels)) counts = [scores[level]["count"] for level in levels] print("{:20} {:<20d} {:<20d} {:<20d} {:<20d} {:<20d}".format("count", *counts)) if etype in ["all", "exec"]: print("===================== EXECUTION ACCURACY =====================") this_scores = [scores[level]["exec"] for level in levels] print( "{:20} {:<20.3f} {:<20.3f} {:<20.3f} {:<20.3f} {:<20.3f}".format( "execution", *this_scores ) ) if etype in ["all", "match"]: print("\n====================== EXACT MATCHING ACCURACY =====================") exact_scores = [scores[level]["exact"] for level in levels] print( "{:20} {:<20.3f} {:<20.3f} {:<20.3f} {:<20.3f} {:<20.3f}".format( "exact match", *exact_scores ) ) print("\n---------------------PARTIAL MATCHING ACCURACY----------------------") for type_ in partial_types: this_scores = [scores[level]["partial"][type_]["acc"] for level in levels] print( "{:20} {:<20.3f} {:<20.3f} {:<20.3f} {:<20.3f} {:<20.3f}".format( type_, *this_scores ) ) print("---------------------- PARTIAL MATCHING RECALL ----------------------") for type_ in partial_types: this_scores = [scores[level]["partial"][type_]["rec"] for level in levels] print( "{:20} {:<20.3f} {:<20.3f} {:<20.3f} {:<20.3f} {:<20.3f}".format( type_, *this_scores ) ) print("---------------------- PARTIAL MATCHING F1 --------------------------") for type_ in partial_types: this_scores = [scores[level]["partial"][type_]["f1"] for level in levels] print( "{:20} {:<20.3f} {:<20.3f} {:<20.3f} {:<20.3f} {:<20.3f}".format( type_, *this_scores ) ) def evaluate(gold, predict, db_dir, etype, kmaps): with open(gold) as f: glist = [l.strip().split("\t") for l in f.readlines() if len(l.strip()) > 0] with open(predict) as f: plist = [l.strip().split("\t") for l in f.readlines() if len(l.strip()) > 0] # plist = [("select max(Share),min(Share) from performance where Type != 'terminal'", "orchestra")] # glist = [("SELECT max(SHARE) , min(SHARE) FROM performance WHERE TYPE != 'Live final'", "orchestra")] evaluator = Evaluator() levels = ["easy", "medium", "hard", "extra", "all"] partial_types = [ "select", "select(no AGG)", "where", "where(no OP)", "group(no Having)", "group", "order", "and/or", "IUEN", "keywords", ] entries = [] scores = {} for level in levels: scores[level] = {"count": 0, "partial": {}, "exact": 0.0} scores[level]["exec"] = 0 for type_ in partial_types: scores[level]["partial"][type_] = { "acc": 0.0, "rec": 0.0, "f1": 0.0, "acc_count": 0, "rec_count": 0, } eval_err_num = 0 for p, g in zip(plist, glist): p_str = p[0] g_str, db = g db_name = db db = os.path.join(db_dir, db, db + ".sqlite") schema = Schema(get_schema(db)) g_sql = get_sql(schema, g_str) hardness = evaluator.eval_hardness(g_sql) scores[hardness]["count"] += 1 scores["all"]["count"] += 1 try: p_sql = get_sql(schema, p_str) except: # If p_sql is not valid, then we will use an empty sql to evaluate with the correct sql p_sql = { "except": None, "from": {"conds": [], "table_units": []}, "groupBy": [], "having": [], "intersect": None, "limit": None, "orderBy": [], "select": [False, []], "union": None, "where": [], } eval_err_num += 1 print("eval_err_num:{}".format(eval_err_num)) # rebuild sql for value evaluation kmap = kmaps[db_name] g_valid_col_units = build_valid_col_units(g_sql["from"]["table_units"], schema) g_sql = rebuild_sql_val(g_sql) g_sql = rebuild_sql_col(g_valid_col_units, g_sql, kmap) p_valid_col_units = build_valid_col_units(p_sql["from"]["table_units"], schema) p_sql = rebuild_sql_val(p_sql) p_sql = rebuild_sql_col(p_valid_col_units, p_sql, kmap) if etype in ["all", "exec"]: exec_score = eval_exec_match(db, p_str, g_str, p_sql, g_sql) if exec_score: scores[hardness]["exec"] += 1.0 scores["all"]["exec"] += 1.0 if etype in ["all", "match"]: exact_score = evaluator.eval_exact_match(p_sql, g_sql) partial_scores = evaluator.partial_scores if exact_score == 0: print("{} pred: {}".format(hardness, p_str)) print("{} gold: {}".format(hardness, g_str)) print("") scores[hardness]["exact"] += exact_score scores["all"]["exact"] += exact_score for type_ in partial_types: if partial_scores[type_]["pred_total"] > 0: scores[hardness]["partial"][type_]["acc"] += partial_scores[type_][ "acc" ] scores[hardness]["partial"][type_]["acc_count"] += 1 if partial_scores[type_]["label_total"] > 0: scores[hardness]["partial"][type_]["rec"] += partial_scores[type_][ "rec" ] scores[hardness]["partial"][type_]["rec_count"] += 1 scores[hardness]["partial"][type_]["f1"] += partial_scores[type_]["f1"] if partial_scores[type_]["pred_total"] > 0: scores["all"]["partial"][type_]["acc"] += partial_scores[type_][ "acc" ] scores["all"]["partial"][type_]["acc_count"] += 1 if partial_scores[type_]["label_total"] > 0: scores["all"]["partial"][type_]["rec"] += partial_scores[type_][ "rec" ] scores["all"]["partial"][type_]["rec_count"] += 1 scores["all"]["partial"][type_]["f1"] += partial_scores[type_]["f1"] entries.append( { "predictSQL": p_str, "goldSQL": g_str, "hardness": hardness, "exact": exact_score, "partial": partial_scores, } ) for level in levels: if scores[level]["count"] == 0: continue if etype in ["all", "exec"]: scores[level]["exec"] /= scores[level]["count"] if etype in ["all", "match"]: scores[level]["exact"] /= scores[level]["count"] for type_ in partial_types: if scores[level]["partial"][type_]["acc_count"] == 0: scores[level]["partial"][type_]["acc"] = 0 else: scores[level]["partial"][type_]["acc"] = ( scores[level]["partial"][type_]["acc"] / scores[level]["partial"][type_]["acc_count"] * 1.0 ) if scores[level]["partial"][type_]["rec_count"] == 0: scores[level]["partial"][type_]["rec"] = 0 else: scores[level]["partial"][type_]["rec"] = ( scores[level]["partial"][type_]["rec"] / scores[level]["partial"][type_]["rec_count"] * 1.0 ) if ( scores[level]["partial"][type_]["acc"] == 0 and scores[level]["partial"][type_]["rec"] == 0 ): scores[level]["partial"][type_]["f1"] = 1 else: scores[level]["partial"][type_]["f1"] = ( 2.0 * scores[level]["partial"][type_]["acc"] * scores[level]["partial"][type_]["rec"] / ( scores[level]["partial"][type_]["rec"] + scores[level]["partial"][type_]["acc"] ) ) print_scores(scores, etype) def eval_exec_match(db, p_str, g_str, pred, gold): """ return 1 if the values between prediction and gold are matching in the corresponding index. Currently not support multiple col_unit(pairs). """ conn = sqlite3.connect(db) cursor = conn.cursor() try: cursor.execute(p_str) p_res = cursor.fetchall() except: return False cursor.execute(g_str) q_res = cursor.fetchall() def res_map(res, val_units): rmap = {} for idx, val_unit in enumerate(val_units): key = ( tuple(val_unit[1]) if not val_unit[2] else (val_unit[0], tuple(val_unit[1]), tuple(val_unit[2])) ) rmap[key] = [r[idx] for r in res] return rmap p_val_units = [unit[1] for unit in pred["select"][1]] q_val_units = [unit[1] for unit in gold["select"][1]] return res_map(p_res, p_val_units) == res_map(q_res, q_val_units) from multiprocessing import Manager, Process def execute(db, p_str, pred, timeout): conn = sqlite3.connect(f"file:{db}?mode=ro", uri=True, timeout=30) cursor = conn.cursor() with Manager() as manager: result = manager.list() def unsafe_execute(result): # sys.stdout = open(os.devnull, "w") sys.stderr = open(os.devnull, "w") try: cursor.execute(p_str) p_res = cursor.fetchall() except sqlite3.OperationalError as e: if "locked" in str(e): print(e) raise ValueError("Invalid") result.append(p_res) p = Process(target=unsafe_execute, args=(result,)) p.start() p.join(timeout=timeout) if p.exitcode != 0: return False, None else: try: p_res = result[0] def res_map(res, val_units): rmap = {} for idx, val_unit in enumerate(val_units): key = ( tuple(val_unit[1]) if not val_unit[2] else (val_unit[0], tuple(val_unit[1]), tuple(val_unit[2])) ) rmap[key] = [r[idx] for r in res] return rmap p_val_units = [unit[1] for unit in pred["select"][1]] return True, res_map(p_res, p_val_units) except: return False, None # Rebuild SQL functions for value evaluation def rebuild_cond_unit_val(cond_unit): if cond_unit is None or not DISABLE_VALUE: return cond_unit not_op, op_id, val_unit, val1, val2 = cond_unit if type(val1) is not dict: val1 = None else: val1 = rebuild_sql_val(val1) if type(val2) is not dict: val2 = None else: val2 = rebuild_sql_val(val2) return not_op, op_id, val_unit, val1, val2 def rebuild_condition_val(condition): if condition is None or not DISABLE_VALUE: return condition res = [] for idx, it in enumerate(condition): if idx % 2 == 0: res.append(rebuild_cond_unit_val(it)) else: res.append(it) return res def rebuild_sql_val(sql): if sql is None or not DISABLE_VALUE: return sql sql["from"]["conds"] = rebuild_condition_val(sql["from"]["conds"]) sql["having"] = rebuild_condition_val(sql["having"]) sql["where"] = rebuild_condition_val(sql["where"]) sql["intersect"] = rebuild_sql_val(sql["intersect"]) sql["except"] = rebuild_sql_val(sql["except"]) sql["union"] = rebuild_sql_val(sql["union"]) return sql # Rebuild SQL functions for foreign key evaluation def build_valid_col_units(table_units, schema): col_ids = [ table_unit[1] for table_unit in table_units if table_unit[0] == TABLE_TYPE["table_unit"] ] prefixs = [col_id[:-2] for col_id in col_ids] valid_col_units = [] for value in schema.idMap.values(): if "." in value and value[: value.index(".")] in prefixs: valid_col_units.append(value) return valid_col_units def rebuild_col_unit_col(valid_col_units, col_unit, kmap): if col_unit is None: return col_unit agg_id, col_id, distinct = col_unit if col_id in kmap and col_id in valid_col_units: col_id = kmap[col_id] if DISABLE_DISTINCT: distinct = None return agg_id, col_id, distinct def rebuild_val_unit_col(valid_col_units, val_unit, kmap): if val_unit is None: return val_unit unit_op, col_unit1, col_unit2 = val_unit col_unit1 = rebuild_col_unit_col(valid_col_units, col_unit1, kmap) col_unit2 = rebuild_col_unit_col(valid_col_units, col_unit2, kmap) return unit_op, col_unit1, col_unit2 def rebuild_table_unit_col(valid_col_units, table_unit, kmap): if table_unit is None: return table_unit table_type, col_unit_or_sql = table_unit if isinstance(col_unit_or_sql, tuple): col_unit_or_sql = rebuild_col_unit_col(valid_col_units, col_unit_or_sql, kmap) return table_type, col_unit_or_sql def rebuild_cond_unit_col(valid_col_units, cond_unit, kmap): if cond_unit is None: return cond_unit not_op, op_id, val_unit, val1, val2 = cond_unit val_unit = rebuild_val_unit_col(valid_col_units, val_unit, kmap) return not_op, op_id, val_unit, val1, val2 def rebuild_condition_col(valid_col_units, condition, kmap): for idx in range(len(condition)): if idx % 2 == 0: condition[idx] = rebuild_cond_unit_col( valid_col_units, condition[idx], kmap ) return condition def rebuild_select_col(valid_col_units, sel, kmap): if sel is None: return sel distinct, _list = sel new_list = [] for it in _list: agg_id, val_unit = it new_list.append((agg_id, rebuild_val_unit_col(valid_col_units, val_unit, kmap))) if DISABLE_DISTINCT: distinct = None return distinct, new_list def rebuild_from_col(valid_col_units, from_, kmap): if from_ is None: return from_ from_["table_units"] = [ rebuild_table_unit_col(valid_col_units, table_unit, kmap) for table_unit in from_["table_units"] ] from_["conds"] = rebuild_condition_col(valid_col_units, from_["conds"], kmap) return from_ def rebuild_group_by_col(valid_col_units, group_by, kmap): if group_by is None: return group_by return [ rebuild_col_unit_col(valid_col_units, col_unit, kmap) for col_unit in group_by ] def rebuild_order_by_col(valid_col_units, order_by, kmap): if order_by is None or len(order_by) == 0: return order_by direction, val_units = order_by new_val_units = [ rebuild_val_unit_col(valid_col_units, val_unit, kmap) for val_unit in val_units ] return direction, new_val_units def rebuild_sql_col(valid_col_units, sql, kmap): if sql is None: return sql sql["select"] = rebuild_select_col(valid_col_units, sql["select"], kmap) sql["from"] = rebuild_from_col(valid_col_units, sql["from"], kmap) sql["where"] = rebuild_condition_col(valid_col_units, sql["where"], kmap) sql["groupBy"] = rebuild_group_by_col(valid_col_units, sql["groupBy"], kmap) sql["orderBy"] = rebuild_order_by_col(valid_col_units, sql["orderBy"], kmap) sql["having"] = rebuild_condition_col(valid_col_units, sql["having"], kmap) sql["intersect"] = rebuild_sql_col(valid_col_units, sql["intersect"], kmap) sql["except"] = rebuild_sql_col(valid_col_units, sql["except"], kmap) sql["union"] = rebuild_sql_col(valid_col_units, sql["union"], kmap) return sql def build_foreign_key_map(entry): cols_orig = entry["column_names_original"] tables_orig = entry["table_names_original"] # rebuild cols corresponding to idmap in Schema cols = [] for col_orig in cols_orig: if col_orig[0] >= 0: t = tables_orig[col_orig[0]] c = col_orig[1] cols.append("__" + t.lower() + "." + c.lower() + "__") else: cols.append("__all__") def keyset_in_list(k1, k2, k_list): for k_set in k_list: if k1 in k_set or k2 in k_set: return k_set new_k_set = set() k_list.append(new_k_set) return new_k_set foreign_key_list = [] foreign_keys = entry["foreign_keys"] for fkey in foreign_keys: key1, key2 = fkey key_set = keyset_in_list(key1, key2, foreign_key_list) key_set.add(key1) key_set.add(key2) foreign_key_map = {} for key_set in foreign_key_list: sorted_list = sorted(list(key_set)) midx = sorted_list[0] for idx in sorted_list: foreign_key_map[cols[idx]] = cols[midx] return foreign_key_map def build_foreign_key_map_from_json(table): with open(table) as f: data = json.load(f) tables = {} for entry in data: tables[entry["db_id"]] = build_foreign_key_map(entry) return tables if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--gold", dest="gold", type=str) parser.add_argument("--pred", dest="pred", type=str) parser.add_argument("--db", dest="db", type=str) parser.add_argument("--table", dest="table", type=str) parser.add_argument("--etype", dest="etype", type=str) args = parser.parse_args() gold = args.gold pred = args.pred db_dir = args.db table = args.table etype = args.etype assert etype in ["all", "exec", "match"], "Unknown evaluation method" kmaps = build_foreign_key_map_from_json(table) evaluate(gold, pred, db_dir, etype, kmaps)
coder_reviewer_reranking-main
utils_sql.py
# Copyright (c) Meta Platforms, Inc. and affiliates. from time import sleep import os import random import openai import re import json def safe_codex_call( args, api_text, temperature=None, stop=None, echo=False, max_tokens=256, api_i=0 ): temperature = temperature if temperature else args.temperature while True: try: if args.model == "codex002": openai.organization = os.getenv(f"OPENAI_ORG{api_i+1}") else: openai.organization = os.getenv("OPENAI_ORG1") codex_response = codex_greedy( api_text, temperature=temperature, codex_config=args.model, stop=stop, echo=echo, max_tokens=max_tokens, ) break except openai.error.InvalidRequestError as e: codex_response = None if isinstance(api_text, list): api_text = [t.replace("\n", "") for t in api_text] else: api_text = api_text.replace("\n", "") print("Invalid Request: Removing newlines") except openai.error.RateLimitError as e: print(type(e), f"API {api_i}:", e, end="\r") sleep(30) api_i = (api_i + 1) % 3 except Exception as e: print(type(e), e) sleep(10) if codex_response is None: codex_text = "" else: codex_text = "".join(codex_response["choices"][0]["logprobs"]["tokens"]) return codex_response, codex_text def codex_greedy( prompt, temperature=0.3, codex_config="codex", stop=None, echo=False, max_tokens=256 ): if stop is None: stop = ["#SOLUTION END", "# SOLUTION END", "SOLUTION END"] if codex_config == "codex001": codex_code = "code-davinci-001" elif codex_config == "codex002": codex_code = "code-davinci-002" elif codex_config == "codex-cushman": codex_code = "code-cushman-001" else: raise ValueError response = openai.Completion.create( engine=codex_code, prompt=prompt, temperature=temperature, stop=stop, max_tokens=max_tokens, top_p=0.95, logprobs=1, frequency_penalty=0, presence_penalty=0, echo=echo, ) return response def write_jsonl(data_list, file_path): with open(file_path, "w") as f: for d in data_list: f.write(json.dumps(d) + "\n") def parse_prompt(prompt, dataset="mbpp"): prompt_data = [] fewshot_examples = [ p.strip() + "</code>" for p in prompt.split("</code>") if len(p) > 1 ] for example in fewshot_examples: example_data = dict() if dataset in ["mbpp", "spider"]: all_fields = ["info", "text", "code"] elif dataset == "nl2bash": all_fields = ["text", "code"] for field in all_fields: field_start = example.index(f"<{field}>") field_end = example.index(f"</{field}>") example_data[field] = example[field_start : field_end + len(f"</{field}>")] prompt_data.append(example_data) return prompt_data def make_new_context(prompt_parse, dataset="mbpp"): without_ref = "" with_ref = "" if dataset == "mbpp": full_prompt_fields = ["code", "info", "text"] elif dataset == "spider": full_prompt_fields = ["info", "code", "text"] else: full_prompt_fields = ["code", "text"] if dataset == "mbpp" or dataset == "nl2bash": partial_prompt_fields = ["code"] elif dataset == "spider": partial_prompt_fields = ["info", "code"] for i, example in enumerate(prompt_parse): for field in full_prompt_fields: with_ref += example[field] + "\n" if i < len(prompt_parse) - 1: for field in full_prompt_fields: without_ref += example[field] + "\n" else: for field in partial_prompt_fields: without_ref += example[field] + "\n" return with_ref.strip(), without_ref.strip() from contextlib import contextmanager import signal class TimeoutException(Exception): pass @contextmanager def time_limit(seconds: float): def signal_handler(signum, frame): raise TimeoutException("Timed out!") signal.setitimer(signal.ITIMER_REAL, seconds) signal.signal(signal.SIGALRM, signal_handler) try: yield finally: signal.setitimer(signal.ITIMER_REAL, 0)
coder_reviewer_reranking-main
utils.py
# Copyright (c) Meta Platforms, Inc. and affiliates. import keyword, sys from pyminifier import analyze from pyminifier.minification import remove_comments_and_docstrings, remove_blank_lines import re RESERVED_WORDS = keyword.kwlist + analyze.builtins def clean_comment(code): code = remove_comments_and_docstrings(code) code = remove_blank_lines(code) return code def remove_print(code): code = re.sub("print(.+)", "print('')", code) code = re.sub("Error(.+)", "Error('')", code) code = re.sub("Exception(.+)", "Exception('')", code) code = re.sub("assert (.+), +['\"].+['\"]", "assert \\1", code) return code
coder_reviewer_reranking-main
pyminifier_canonicalize.py
# Copyright (c) Meta Platforms, Inc. and affiliates. import os import tempfile from datasets import load_metric from tqdm import tqdm import pickle as pkl from data import MBPPGoogleDataset from execution import Command import sys from utils import time_limit """ dataset keys: src, trg_prediction, reference """ def evaluate_charbleu(dataset): bleu = load_metric("bleu") predictions = [[ch for ch in item["trg_prediction"]] for item in dataset] references = [[[ch for ch in item["reference"]]] for item in dataset] return bleu.compute(predictions=predictions, references=references) """ dataset keys: src, trg_prediction, reference (only trg_prediction useful) """ def evaluate_spider_with_cached_results(selected): all_pred_results = [item["execution_result"] for item in selected] all_gold_results = pkl.load( open( "./dataset/spider/cached_gold_results.pkl", "rb", ) ) total_correct = 0 for p_res, g_res in tqdm( zip(all_pred_results, all_gold_results), total=len(all_gold_results), ): total_correct += int(p_res[1] == g_res) return total_correct / len(all_gold_results) def evaluate_one_mbpp(args, tempdir, dataset, timeout): i, item = args if "execution_result_full_pass" in dataset[i]: return int( all( isinstance(x[1], bool) and x[1] == True for x in dataset[i]["execution_result_full_pass"] ) ) else: test_cases = item["test_list"] test_setups = item["test_setup_code"] code = dataset[i]["trg_prediction"] # write code to file with open(f"{tempdir.name}/code-{i}.py", "w") as fout: print(code, file=fout) print(test_setups, file=fout) for case in test_cases: print(case, file=fout) fout.close() command = Command(f"python {tempdir.name}/code-{i}.py >/dev/null 2>&1") execution_result = command.run(timeout=timeout) == 0 return execution_result from functools import partial from multiprocessing import Pool """ dataset keys: src, trg_prediction, reference (only trg_prediction useful) """ def evaluate_google_mbpp( dataset, reference_path, split="test", timeout=10, return_details=False, num_procs=1, verbose=False, ): references = MBPPGoogleDataset(reference_path) assert len(dataset) == len(references.raw_data[split]) tempdir = tempfile.TemporaryDirectory() passed_information = list() partial_evalutate_one = partial( evaluate_one_mbpp, tempdir=tempdir, dataset=dataset, timeout=timeout ) if num_procs > 1: with Pool(processes=num_procs) as pool: for result_json in tqdm( pool.imap( partial_evalutate_one, list(enumerate(references.raw_data[split])) ), total=len(references.raw_data[split]), leave=False, disable=not verbose, ): passed_information.append(result_json) else: for args in tqdm( list(enumerate(references.raw_data[split])), disable=not verbose ): passed_information.append(partial_evalutate_one(args)) tempdir.cleanup() if return_details: return passed_information else: return sum(passed_information) / len(passed_information) def evaluate_humaneval(dataset): all_passed = [d["execution_result_full_pass"] for d in dataset] return sum(all_passed) / len(all_passed)
coder_reviewer_reranking-main
evaluate.py
# Copyright (c) Meta Platforms, Inc. and affiliates. import bashlex import collections import json import pickle import numpy as np import os import random from glob import glob from nltk.translate.bleu_score import sentence_bleu from evaluate import ( evaluate_charbleu, evaluate_google_mbpp, evaluate_spider_with_cached_results, evaluate_humaneval, ) from tqdm import tqdm, trange from pyminifier_canonicalize import clean_comment import torch from argparse import ArgumentParser from pathlib import Path from functools import partial from multiprocessing import Pool from datasets import load_metric class MultiSampleSelector(object): def __init__( self, paths, split="dev", tag="", model="", dataset="", verbose=False, no_rejection=False, ): self.paths = ( list(sorted(glob(paths, recursive=True))) if isinstance(paths, str) else list(sorted(paths)) ) self.split = split self.data = collections.defaultdict(list) self.args = collections.defaultdict(list) self.tag = tag self.model = model self.dataset = dataset self.verbose = verbose for i, path in tqdm( enumerate(self.paths), total=len(self.paths), desc="loading jsons", disable=not self.verbose, ): self.args[i] = pickle.load(open(f"{self.paths[0]}/configs.pkl", "rb")) idx = 0 if self.tag != "": file_path = f"{path}/{split}-{idx}-{tag}.jsonl" else: file_path = f"{path}/{split}-{idx}.jsonl" while os.path.exists(file_path): self.data[i, idx].extend([json.loads(x) for x in open(file_path)]) idx += 1 if self.tag != "": file_path = f"{path}/{split}-{idx}-{tag}.jsonl" else: file_path = f"{path}/{split}-{idx}.jsonl" print(f"{len(self.data)} cached samples") for path_id, sample_id in tqdm( self.data, desc="loading logprobs", disable=not self.verbose ): if ( self.paths[path_id].find("nl2bash") != -1 ): # NL2bash data, exec simulation if self.tag != "": file_name = f"{path}/{split}-{sample_id}-{tag}" else: file_name = f"{path}/{split}-{sample_id}" exec_results = pickle.load(open(f"{file_name}.exec.pkl", "rb")) simulate_exec_results = pickle.load( open(f"{file_name}.exec.simulate.pkl", "rb") ) splitted_exec_results = pickle.load( open(f"{file_name}.exec.splitted.pkl", "rb") ) char_bleus = pickle.load(open(f"{file_name}.exec.bleu.pkl", "rb")) for item_i, item in enumerate(self.data[path_id, sample_id]): if no_rejection: item["not_degenerate"] = True else: # implementing degenerate solution rejection if self.dataset in ["codet_humaneval", "mbpp_sanitized"]: item["not_degenerate"] = filter_empty( item, remove_function_header=False ) and filter_repeat(item["trg_prediction"]) elif self.dataset in ["mbpp"]: item["not_degenerate"] = filter_empty( item, remove_function_header=True ) elif self.dataset in ["spider", "nl2bash"]: item["not_degenerate"] = len( item["trg_prediction"] ) != 0 and filter_repeat(item["trg_prediction"]) else: raise ValueError("Invalid Dataset.") avg_logprob, sum_logprob = self.extract_logprob_stats(item, path_id) item["avg_logprob"] = avg_logprob item["sum_logprob"] = sum_logprob reverse_logprob = self.extract_reverse_logprob(item, path_id) ( item["sum_reverse_logprob"], item["avg_reverse_logprob"], ) = reverse_logprob if ( self.paths[path_id].find("nl2bash") != -1 ): # NL2bash data, exec simulation item["executable"] = exec_results[item_i] item["trg_prediction_splitted"] = splitted_exec_results[item_i] item["execution_result_simulated"] = simulate_exec_results[item_i] item["charbleu"] = char_bleus[item_i] def extract_reverse_logprob(self, item, path_id): if "prompt_reverse_logprobs" not in item: return 0, 0 logprobs = item["prompt_reverse_logprobs"] return np.sum(logprobs), np.mean(logprobs) def extract_logprob_stats(self, item, path_id): current_seq = "" if "codex" in self.model: extracted_position = None for i, _ in enumerate(item["tokens"]): current_seq += item["tokens"][i] end_template = self.args[path_id].end_template if isinstance(end_template, list): end_template = "" if ( current_seq.find(item["trg_prediction"]) != -1 and current_seq.find(end_template) != -1 ): extracted_position = i + 1 break logprobs = ( item["logprobs"][:extracted_position] if extracted_position is not None else item["logprobs"] ) logprobs = list( filter(lambda x: x < 0, logprobs) ) # handle potential codex bug on positive log probability else: logprobs = item["logprobs"] return np.mean(logprobs), np.sum(logprobs) def select( self, ids=None, key_extractor=lambda x: x["avg_logprob"], return_keys=False ): if ids is None: ids = self.data.keys() ids = list(sorted(ids)) n_examples = len(self.data[ids[0]]) selected_examples = list() sample_keys = collections.defaultdict(list) for i in range(n_examples): max_key = None selected_item = None for idx in ids: item = self.data[idx][i] key = key_extractor(item) sample_keys[idx].append(key) if max_key is None or key > max_key: max_key = key selected_item = item assert selected_item is not None selected_examples.append(selected_item) if return_keys: return selected_examples, sample_keys else: return selected_examples class ExecutionBasedMultiSampleSelector(MultiSampleSelector): def __init__( self, paths, split="dev", execution_type=None, tag="", model="", verbose=False, dataset="", no_rejection=False, ): super().__init__( paths, split=split, tag=tag, model=model, verbose=verbose, dataset=dataset, no_rejection=no_rejection, ) self.execution_type = execution_type load_execution(self.data, self.paths, split, self.tag) class IntraMultiSampleSelector(MultiSampleSelector): def __init__( self, paths, split="dev", tag="", model="", verbose=False, dataset="", no_rejection=False, ): super().__init__( paths, split=split, tag=tag, model=model, verbose=verbose, dataset=dataset, no_rejection=no_rejection, ) def select( self, ids=None, key_extractor=None, second_key_extractor=None, return_keys=False, quantile_threshold=None, ): if ids is None: ids = self.data.keys() elif isinstance(ids, int): ids = [ (i, j) for i in set(x[0] for x in self.data.keys()) for j in range(ids) ] ids = list(sorted(ids)) id_set = set(ids) sample_keys = collections.defaultdict(list) # print(f'Selecting Samples from IDs: {ids}') n_examples = len(self.data[ids[0]]) selected_examples = list() for i in range(n_examples): max_key = None selected_item = None if quantile_threshold is not None: filtered_ids = [] all_second_key = [] for idx in ids: selected_item = None item = self.data[idx][i] all_second_key.append( second_key_extractor(item) if second_key_extractor is not None else 0 ) threshold = np.quantile(all_second_key, quantile_threshold) for idx_i, idx in enumerate(ids): if all_second_key[idx_i] >= threshold: filtered_ids.append(idx) else: filtered_ids = ids for idx in filtered_ids: item = self.data[idx][i] first_keys = list() for grndtruth_idx in filtered_ids: grndtruth_item = self.data[grndtruth_idx][i] key = key_extractor(item, grndtruth_item) first_keys.append(key) first_key = sum(first_keys) second_key = ( second_key_extractor(item) if second_key_extractor is not None else 0 ) current_key = (first_key, second_key) item["mbr_key"] = current_key sample_keys[idx].append(current_key) if max_key is None or current_key > max_key: max_key = current_key selected_item = item assert selected_item is not None selected_examples.append(selected_item) if return_keys: return selected_examples, sample_keys else: return selected_examples class ExecutionBasedIntraMultiSampleSelector(IntraMultiSampleSelector): def __init__( self, paths, split="dev", execution_type=None, tag="", model="", verbose=False, dataset="", no_rejection=False, ): super().__init__( paths, split=split, tag=tag, model=model, verbose=verbose, dataset=dataset, no_rejection=no_rejection, ) self.execution_type = execution_type load_execution(self.data, self.paths, split, self.tag) def filter_empty(x, remove_function_header=False): code = x["trg_prediction"] if remove_function_header: code = "\n".join( [l for l in code.split("\n") if not l.strip().startswith("def")] ) try: code = clean_comment(code) except: code = "" return code.strip() not in ["", "pass", "return"] def filter_repeat(x, threshold=0.25): import zlib bytes_x = bytes(x, encoding="utf-8") comp_x = zlib.compress(bytes_x) return len(comp_x) / len(bytes_x) > threshold def load_execution(data_dict, paths, split, tag): if "spider" in paths[0]: exec_list = [ ("exec.pkl", "execution_result"), ] else: exec_list = [ ("exec.pkl", "execution_result"), ("execfull.pkl", "execution_result_full"), ("execfullpass.pkl", "execution_result_full_pass"), ("gen.execfull.pkl", "gen_execution_result_full"), ] for suffix, result_name in exec_list: for i, idx in data_dict: if tag == "": out_name = f"{split}-{idx}.{suffix}" else: out_name = f"{split}-{idx}-{tag}.{suffix}" path = paths[i] if suffix != "gen.execfull.pkl" or os.path.exists(f"{path}/{out_name}"): execution_results = pickle.load(open(f"{path}/{out_name}", "rb")) assert len(execution_results) == len(data_dict[i, idx]) for j, execution_result in enumerate(execution_results): data_dict[i, idx][j][result_name] = execution_result """equivalence checking functions""" # base equavalence checking function def single_exec_result_matching(exec_x, exec_y, good_execution_result): try: if ( exec_x[0] == good_execution_result and exec_y[0] == good_execution_result and exec_x[1] == exec_y[1] ): return 1 else: return 0 except: return 0 def multi_exec_result_matching(exec_x, exec_y, good_execution_result): try: same_output_count = 0 if exec_x[0] == good_execution_result and exec_y[0] == good_execution_result: for ex, ey in exec_x[0], exec_y[1]: same_output_count += 1 return same_output_count else: return 0 except: return 0 # first assertion call matching def execution_selection_function(x, y, good_execution_result=0): exec_x, exec_y = x["execution_result"], y["execution_result"] return single_exec_result_matching(exec_x, exec_y, good_execution_result) def multi_execution_selection_function(x, y, good_execution_result=0): exec_x, exec_y = x["gen_execution_result_full"], y["gen_execution_result_full"] return sum( [ single_exec_result_matching(single_x, single_y, good_execution_result) for single_x, single_y in zip(exec_x, exec_y) ] ) # just executability checking def executability_selection_function(x, good_execution_result=0): exec_res = x["execution_result"] return exec_res[0] == good_execution_result def multi_executability_selection_function(x, good_execution_result=0): exec_res = x["gen_execution_result_full"] return sum([e[0] == good_execution_result for e in exec_res]) def bleu_selection_function(x, y): return sentence_bleu( [[ch for ch in x["trg_prediction"]]], [ch for ch in y["trg_prediction"]] ) def token_bleu_selection_function(x, y): return sentence_bleu([x["trg_prediction"].split()], y["trg_prediction"].split()) def bash_execution_tokenbleu_selection_function(x, y): if not x["executable"] or not y["executable"]: return 0 x = x["trg_prediction_splitted"] y = y["trg_prediction_splitted"] return sentence_bleu([x], y) def get_mbpp_selector( criterion, mbpp_good_execution_result, use_multi_assertions=False, remove_function_header=False, ): if "$" in criterion: criterion = criterion.split("$")[0] secondary_key_function = None if not use_multi_assertions: exec_func = execution_selection_function else: exec_func = multi_execution_selection_function mbr_function = lambda x, y: all( [ exec_func(x, y, mbpp_good_execution_result), x["not_degenerate"], y["not_degenerate"], ] ) if not use_multi_assertions: executability_func = executability_selection_function else: executability_func = multi_executability_selection_function if criterion == "oracle": def get_oracle(x): if isinstance(x["execution_result_full_pass"], bool): return int(x["execution_result_full_pass"]) elif isinstance(x["execution_result_full_pass"], list): return int( all( isinstance(exec_result[1], bool) and exec_result[1] == True for exec_result in x["execution_result_full_pass"] ) ) sample_selection_function = get_oracle elif criterion == "mbr_exec": sample_selection_function = mbr_function secondary_key_function = lambda x: x["sum_logprob"] elif criterion in [ "sum_logprob", "avg_logprob", "avg_reverse_logprob", "sum_reverse_logprob", ]: criterion = criterion.split("-")[-1] sample_selection_function = lambda x: ( x["not_degenerate"], x[criterion], ) elif criterion == "random": sample_selection_function = lambda x: ( x["not_degenerate"], random.random(), ) elif criterion.startswith("avgreverselogprob-ensemble#"): alpha = float(criterion.split("#")[1]) sample_selection_function = lambda x: ( x["not_degenerate"], x["avg_reverse_logprob"] * alpha + x["avg_logprob"] * (1 - alpha), ) elif criterion.startswith("sumreverselogprob-ensemble#"): alpha = float(criterion.split("#")[1]) sample_selection_function = lambda x: ( x["not_degenerate"], x["sum_reverse_logprob"] * alpha + x["sum_logprob"] * (1 - alpha), ) elif criterion in [ "executability-sum_logprob", "executability-avg_logprob", "executability-avg_reverse_logprob", "executability-sum_reverse_logprob", ]: criterion = criterion.split("-")[-1] sample_selection_function = lambda x: ( x["not_degenerate"], executability_func(x, mbpp_good_execution_result), x[criterion], ) elif criterion == "executability-random": sample_selection_function = lambda x: ( x["not_degenerate"], executability_func(x, mbpp_good_execution_result), random.random(), ) elif criterion.startswith("executability-avgreverselogprob-ensemble#"): alpha = float(criterion.split("#")[1]) sample_selection_function = lambda x: ( executability_func(x, mbpp_good_execution_result), x["not_degenerate"], x["avg_reverse_logprob"] * alpha + x["avg_logprob"] * (1 - alpha), ) elif criterion.startswith("executability-sumreverselogprob-ensemble#"): alpha = float(criterion.split("#")[1]) sample_selection_function = lambda x: ( executability_func(x, mbpp_good_execution_result), x["not_degenerate"], x["sum_reverse_logprob"] * alpha + x["sum_logprob"] * (1 - alpha), ) else: raise ValueError(f"Unknown criterion: {criterion}") return sample_selection_function, secondary_key_function """ select and evaluate a group in batch required keys: data_split: 'train', 'dev' or 'test' temperature: 0.1 .. 1.0 criterion: 'mbr_exec' ... see full options in the function data_path: root data path for the task n_samples: number of candidates rand_seed: random seed for one experiment """ def bootstrap_mbpp(args): mbpp_good_execution_result = 0 data_path = f"{args.data_path}/seed-*/**/*-{args.temperature}/" multisample_selector = ExecutionBasedMultiSampleSelector( data_path, args.data_split, "mbpp", tag=args.tag, model=args.model, dataset="mbpp", verbose=args.verbose, no_rejection=args.no_rejection, ) intrasample_selector = ExecutionBasedIntraMultiSampleSelector( data_path, args.data_split, "mbpp", tag=args.tag, model=args.model, dataset="mbpp", verbose=args.verbose, no_rejection=args.no_rejection, ) id_keys = list(multisample_selector.data.keys()) acc_dict = collections.defaultdict(list) std_dict = collections.defaultdict(list) for crit in args.criteria: sample_selection_function, secondary_key_function = get_mbpp_selector( crit, mbpp_good_execution_result, remove_function_header=True ) if "mbr" in crit: selector = intrasample_selector else: selector = multisample_selector step_results = collections.defaultdict(list) for sub_n_samples in trange( args.num_samples_start, args.num_samples_end, args.num_samples_gap ): random.seed(args.seed) np.random.seed(args.seed) for bootstrap_i in range(args.num_bootstraps): # id_is = np.random.choice(list(range(len(id_keys))), size=sub_n_samples, replace=True) # ids = [id_keys[i] for i in id_is] ids = random.sample(id_keys, sub_n_samples) if secondary_key_function is not None: if "$" in crit: quantile_threshold = float(crit.split("$")[1]) else: quantile_threshold = None selected = selector.select( ids, sample_selection_function, secondary_key_function, quantile_threshold=quantile_threshold, ) else: selected = selector.select(ids, sample_selection_function) result = evaluate_google_mbpp( selected, "dataset/mbpp/mbpp.jsonl", "test", verbose=args.verbose ) step_results[sub_n_samples].append(result) for k, v in step_results.items(): acc_dict[crit].append(np.mean(v)) std_dict[crit].append(np.std(v)) return (acc_dict, std_dict) def get_nl2bash_selector(criterion): import random secondary_key_function = None if criterion == "oracle": sample_selection_function = lambda x: x["charbleu"] elif criterion == "mbr_bleu": raise NotImplementedError # sample_selection_function = lambda x, y: bleu_selection_function(x, y, tag=tag, model=model) elif criterion == "mbr_tokenbleu": sample_selection_function = lambda x, y: all( [ token_bleu_selection_function(x, y), x["not_degenerate"], y["not_degenerate"], ] ) elif criterion == "mbr_exec_tokenbleu": sample_selection_function = lambda x, y: all( [ bash_execution_tokenbleu_selection_function(x, y), x["not_degenerate"], y["not_degenerate"], ] ) secondary_key_function = lambda x: x["sum_logprob"] elif criterion in [ "sum_logprob", "avg_logprob", "avg_reverse_logprob", "sum_reverse_logprob", ]: criterion = criterion.split("-")[-1] sample_selection_function = lambda x: ( x["not_degenerate"], x[criterion], ) elif criterion == "random": sample_selection_function = lambda x: ( x["not_degenerate"], random.random(), ) elif criterion.startswith("avgreverselogprob-ensemble#"): alpha = float(criterion.split("#")[1]) sample_selection_function = lambda x: ( x["not_degenerate"], x["avg_reverse_logprob"] * alpha + x["avg_logprob"] * (1 - alpha), ) elif criterion.startswith("sumreverselogprob-ensemble#"): alpha = float(criterion.split("#")[1]) sample_selection_function = lambda x: ( x["not_degenerate"], x["sum_reverse_logprob"] * alpha + x["sum_logprob"] * (1 - alpha), ) elif criterion in [ "executability-sum_logprob", "executability-avg_logprob", "executability-avg_reverse_logprob", "executability-sum_reverse_logprob", ]: criterion = criterion.split("-")[-1] sample_selection_function = lambda x: ( x["not_degenerate"], x["executable"], x[criterion], ) elif criterion == "executability-random": sample_selection_function = lambda x: ( x["not_degenerate"], x["executable"], random.random(), ) elif criterion.startswith("executability-avgreverselogprob-ensemble#"): alpha = float(criterion.split("#")[1]) sample_selection_function = lambda x: ( x["executable"], x["not_degenerate"], x["avg_reverse_logprob"] * alpha + x["avg_logprob"] * (1 - alpha), ) elif criterion.startswith("executability-sumreverselogprob-ensemble#"): alpha = float(criterion.split("#")[1]) sample_selection_function = lambda x: ( x["executable"], x["not_degenerate"], x["sum_reverse_logprob"] * alpha + x["sum_logprob"] * (1 - alpha), ) else: raise ValueError(f"Unknown criterion: {criterion}") return sample_selection_function, secondary_key_function def bootstrap_nl2bash(args): data_path = f"{args.data_path}/seed-*/**/*-{args.temperature}/" secondary_key_function = None intra_selector = IntraMultiSampleSelector( data_path, args.data_split, tag=args.tag, model=args.model, verbose=args.verbose, dataset="nl2bash", no_rejection=args.no_rejection, ) multi_selector = MultiSampleSelector( data_path, args.data_split, tag=args.tag, model=args.model, verbose=args.verbose, dataset="nl2bash", no_rejection=args.no_rejection, ) id_keys = list(intra_selector.data.keys()) acc_dict = collections.defaultdict(list) std_dict = collections.defaultdict(list) for crit in args.criteria: sample_selection_function, secondary_key_function = get_nl2bash_selector(crit) if "mbr" in crit: selector = intra_selector else: selector = multi_selector step_results = collections.defaultdict(list) for sub_n_samples in trange( args.num_samples_start, args.num_samples_end, args.num_samples_gap ): random.seed(args.seed) np.random.seed(args.seed) for bootstrap_i in range(args.num_bootstraps): ids = random.sample(id_keys, sub_n_samples) if secondary_key_function is not None: selected = selector.select( ids, sample_selection_function, secondary_key_function ) else: selected = selector.select(ids, sample_selection_function) result = evaluate_charbleu(selected)["bleu"] step_results[sub_n_samples].append(result) for k, v in step_results.items(): acc_dict[crit].append(np.mean(v)) std_dict[crit].append(np.std(v)) return acc_dict, std_dict def bootstrap_human_eval(args): humaneval_good_execution_result = 0 data_path = f"{args.data_path}/seed-*/0-shot/*-{args.temperature}" if args.top_p != 1.0: data_path += f"-p{args.top_p}" if args.max_tokens != 512: data_path += f"-max{args.max_tokens}" multisample_selector = ExecutionBasedMultiSampleSelector( data_path, args.data_split, "humaneval", tag=args.tag, model=args.model, verbose=args.verbose, dataset=args.dataset, no_rejection=args.no_rejection, ) intrasample_selector = ExecutionBasedIntraMultiSampleSelector( data_path, args.data_split, "humaneval", tag=args.tag, model=args.model, verbose=args.verbose, dataset=args.dataset, no_rejection=args.no_rejection, ) id_keys = list(multisample_selector.data.keys()) acc_dict = collections.defaultdict(list) std_dict = collections.defaultdict(list) for crit in args.criteria: sample_selection_function, secondary_key_function = get_mbpp_selector( crit, humaneval_good_execution_result, use_multi_assertions=args.use_generated_assertions, ) if "mbr" in crit: selector = intrasample_selector else: selector = multisample_selector step_results = collections.defaultdict(list) for sub_n_samples in trange( args.num_samples_start, args.num_samples_end, args.num_samples_gap ): random.seed(args.seed) np.random.seed(args.seed) for bootstrap_i in range(args.num_bootstraps): ids = random.sample(id_keys, sub_n_samples) # id_is = np.random.choice(list(range(len(id_keys))), size=sub_n_samples, replace=True) # ids = [id_keys[i] for i in id_is] if secondary_key_function is not None: if "$" in crit: quantile_threshold = float(crit.split("$")[1]) else: quantile_threshold = None selected = selector.select( ids, sample_selection_function, secondary_key_function, quantile_threshold=quantile_threshold, ) else: selected = selector.select(ids, sample_selection_function) result = evaluate_humaneval(selected) step_results[sub_n_samples].append(result) for k, v in step_results.items(): acc_dict[crit].append(np.mean(v)) std_dict[crit].append(np.std(v)) return (acc_dict, std_dict) def get_spider_selector(criterion, spider_good_execution_result=True): import random secondary_key_function = None if criterion == "mbr_exec": sample_selection_function = lambda x, y: execution_selection_function( x, y, spider_good_execution_result ) secondary_key_function = lambda x: x["sum_logprob"] elif criterion == "mbr_exec_reverse": sample_selection_function = lambda x, y: execution_selection_function( x, y, spider_good_execution_result ) secondary_key_function = lambda x: x["avg_reverse_logprob"] elif criterion == "mbr_exec_avglogp": sample_selection_function = lambda x, y: execution_selection_function( x, y, spider_good_execution_result ) secondary_key_function = lambda x: x["avg_logprob"] elif criterion == "random": sample_selection_function = lambda x: random.random() elif criterion in [ "sum_logprob", "avg_logprob", "avg_reverse_logprob", "sum_reverse_logprob", ]: sample_selection_function = lambda x: x[criterion] elif criterion.startswith("avgreverselogprob-ensemble#"): alpha = float(criterion.split("#")[1]) sample_selection_function = lambda x: x["avg_reverse_logprob"] * alpha + x[ "avg_logprob" ] * (1 - alpha) elif criterion.startswith("sumreverselogprob-ensemble#"): alpha = float(criterion.split("#")[1]) sample_selection_function = lambda x: x["sum_reverse_logprob"] * alpha + x[ "sum_logprob" ] * (1 - alpha) elif criterion == "mbr_bleu": sample_selection_function = lambda x, y: bleu_selection_function(x, y) elif criterion == "mbr_tokenbleu": sample_selection_function = lambda x, y: token_bleu_selection_function(x, y) elif criterion in [ "executability-sum_logprob", "executability-avg_logprob", "executability-avg_reverse_logprob", "executability-sum_reverse_logprob", ]: criterion = criterion.split("-")[1] sample_selection_function = lambda x: ( executability_selection_function(x, spider_good_execution_result), x[criterion], ) elif criterion == "executability-random": sample_selection_function = lambda x: ( executability_selection_function(x, spider_good_execution_result), random.random(), ) elif criterion == "executability-mbr_bleu": sample_selection_function = ( lambda x, y: bleu_selection_function(x, y) * x["execution_result"][0] * y["execution_result"][0] ) elif criterion == "executability-mbr_tokenbleu": sample_selection_function = ( lambda x, y: token_bleu_selection_function(x, y) * x["execution_result"][0] * y["execution_result"][0] ) elif criterion.startswith("executability-avgreverselogprob-ensemble#"): alpha = float(criterion.split("#")[1]) sample_selection_function = lambda x: ( executability_selection_function(x, spider_good_execution_result), x["avg_reverse_logprob"] * alpha + x["avg_logprob"] * (1 - alpha), ) elif criterion.startswith("executability-sumreverselogprob-ensemble#"): alpha = float(criterion.split("#")[1]) sample_selection_function = lambda x: ( executability_selection_function(x, spider_good_execution_result), x["sum_reverse_logprob"] * alpha + x["sum_logprob"] * (1 - alpha), ) else: raise ValueError(f"Unknown criterion: {criterion}") return sample_selection_function, secondary_key_function def bootstrap_spider(args): spider_good_execution_result = True data_path = f"{args.data_path}/seed-*/**/*-{args.temperature}/" intrasample_selector = ExecutionBasedIntraMultiSampleSelector( data_path, "dev", "spider", tag=args.tag, model=args.model, verbose=args.verbose, dataset="spider", no_rejection=args.no_rejection, ) multisample_selector = ExecutionBasedMultiSampleSelector( data_path, "dev", "spider", tag=args.tag, model=args.model, verbose=args.verbose, dataset="spider", no_rejection=args.no_rejection, ) # pre-execution for faster evaluation id_keys = list(multisample_selector.data.keys()) acc_dict = collections.defaultdict(list) std_dict = collections.defaultdict(list) # preloading spider data to reduce io from dataset.spider_official.evaluation import ( evaluate, build_foreign_key_map_from_json, ) kmaps = build_foreign_key_map_from_json( "/private/home/tianyizzz/projects/mbr-exec/dataset/spider/tables.json" ) with open("dataset/spider/dev_gold.sql") as f: glist = [l.strip().split("\t") for l in f.readlines() if len(l.strip()) > 0] all_args = [] flat_accs = [] for crit in args.criteria: if "mbr" in crit: selector = intrasample_selector else: selector = multisample_selector for sub_n_samples in range( args.num_samples_start, args.num_samples_end, args.num_samples_gap ): random.seed(args.seed) np.random.seed(args.seed) for bootstrap_i in range(args.num_bootstraps): id_is = np.random.choice( list(range(len(id_keys))), size=sub_n_samples, replace=True ) ids = [id_keys[i] for i in id_is] all_args.append( ( ids, crit, selector, sub_n_samples * args.num_bootstraps + bootstrap_i, kmaps, glist, ) ) if args.num_procs > 1: print(f"running with {args.num_procs} processes.") from multiprocessing.pool import ThreadPool as Pool with Pool(processes=args.num_procs) as pool: for acc in tqdm( pool.imap(evaluate_spider_one, all_args, chunksize=1), total=len(all_args), desc=f"{crit}", ): flat_accs.append(acc) else: for data in tqdm(all_args, total=len(all_args)): flat_accs.append(evaluate_spider_one(data)) acc_idx = 0 for crit in args.criteria: step_results = collections.defaultdict(list) for sub_n_samples in trange( args.num_samples_start, args.num_samples_end, args.num_samples_gap ): for bootstrap_i in range(args.num_bootstraps): step_results[sub_n_samples].append(flat_accs[acc_idx]) acc_idx += 1 for k, v in step_results.items(): acc_dict[crit].append(np.mean(v)) std_dict[crit].append(np.std(v)) return (acc_dict, std_dict) def evaluate_spider_one(args): ids, crit, selector, bootstrap_i, kmaps, glist = args sample_selection_function, secondary_key_function = get_spider_selector(crit) if secondary_key_function is not None: selected = selector.select( ids, sample_selection_function, secondary_key_function ) else: selected = selector.select(ids, sample_selection_function) acc = evaluate_spider_with_cached_results(selected) return acc if __name__ == "__main__": parser = ArgumentParser() parser.add_argument("--tag", type=str, default="") parser.add_argument("--out_dir", type=str, default="./result_db") parser.add_argument( "--model", type=str, choices=[ "codegen-2B", "codegen-2B-half", "codegen-6B", "codegen-6B-half", "codegen-16B-half", "incoder-1B", "incoder-1B-half", "incoder-6B", "incoder-6B-half", "codex001", "codex002", "codex-cushman", ], ) parser.add_argument( "--dataset", type=str, default="mbpp", choices=[ "mbpp", "spider", "nl2bash", "humaneval", "codet_humaneval", "mbpp_sanitized", ], ) parser.add_argument("--num_samples_start", type=int, default=24) parser.add_argument("--num_samples_end", type=int, default=25) parser.add_argument("--num_samples_gap", type=int, default=1) parser.add_argument("--num_procs", type=int, default=1) parser.add_argument("--num_bootstraps", type=int, default=1) parser.add_argument("--temperature", type=float, default=0.3) parser.add_argument("--max_tokens", type=int, default=512) parser.add_argument("--top_p", type=float, default=1.0) parser.add_argument("--verbose", default=False, action="store_true") parser.add_argument("--seed", type=int, default=1) parser.add_argument("--grid_search_alpha", action="store_true", default=False) parser.add_argument("--ablate", action="store_true", default=False) parser.add_argument("--no-rejection", action="store_true", default=False) parser.add_argument( "--use_generated_assertions", action="store_true", default=False ) parser.add_argument( "--data_path", type=str, default="samples", ) args = parser.parse_args() if args.temperature > 0: base_crits = ["sum_logprob", "avg_logprob", "avg_reverse_logprob", "random"] if args.grid_search_alpha: for i in range(1, 10): base_crits.append( f"sumreverselogprob-ensemble#0.{i}", ) for i in range(1, 10): base_crits.append( f"avgreverselogprob-ensemble#0.{i}", ) else: base_crits.append( f"sumreverselogprob-ensemble#0.5", ) base_crits.append( f"avgreverselogprob-ensemble#0.5", ) all_crits = base_crits + ["executability-" + b for b in base_crits] else: all_crits = ["sum_logprob"] args.criteria = all_crits args.data_path = f"{args.data_path}/{args.model}/{args.dataset}" if args.dataset == "spider": args.criteria = all_crits + ["mbr_exec"] acc_dict, std_dict = bootstrap_spider(args) elif args.dataset == "mbpp": args.criteria = args.criteria + ["mbr_exec", "oracle"] args.data_split = "test" acc_dict, std_dict = bootstrap_mbpp(args) elif "humaneval" in args.dataset or args.dataset == "mbpp_sanitized": args.criteria = args.criteria + ["mbr_exec", "oracle"] args.data_split = "test" acc_dict, std_dict = bootstrap_human_eval(args) elif args.dataset == "nl2bash": args.criteria = args.criteria + ["mbr_exec_tokenbleu", "oracle"] args.data_split = "dev" acc_dict, std_dict = bootstrap_nl2bash(args) else: raise ValueError if args.tag != "": out_path = Path( f"{args.out_dir}/{args.dataset}-{args.model}-temp{args.temperature}-{args.tag}" ) else: out_path = Path( f"{args.out_dir}/{args.dataset}-{args.model}-temp{args.temperature}" ) out_path.mkdir(parents=True, exist_ok=True) torch.save(acc_dict, out_path / "acc.pt") torch.save(std_dict, out_path / "std.pt") print(f"saving to {out_path}") for crit in args.criteria: print(crit, f"{acc_dict[crit][-1]:.4f} {std_dict[crit][-1]:.2f}")
coder_reviewer_reranking-main
sample_selectors.py
# Copyright (c) Meta Platforms, Inc. and affiliates. from pathlib import Path import os from glob import glob from argparse import ArgumentParser import html import json from utils import * from tqdm import tqdm, trange from functools import partial from utils import write_jsonl, parse_prompt, make_new_context from pyminifier_canonicalize import remove_print, clean_comment parser = ArgumentParser() parser.add_argument("--model", type=str, default="codex") parser.add_argument( "--dataset", type=str, default="mbpp", choices=["mbpp", "spider", "nl2bash"] ) parser.add_argument("--tag", type=str, default="") parser.add_argument("--split", type=str, default="test") parser.add_argument("--batch_size", type=int, default=20) parser.add_argument("--max_tokens", type=int, default=512) parser.add_argument("--top_p", type=float, default=1.0) parser.add_argument("--num_samples", type=int, default=5) parser.add_argument("--num_procs", type=int, default=40) parser.add_argument("--canonicalize", action="store_true", default=False) parser.add_argument( "--data_path", type=str, default="/private/home/tianyizzz/projects/mbr-exec-data/mbr-exec-release/", ) parser.add_argument("--temperature", type=float, default=0.3) args = parser.parse_args() args.data_path = Path(args.data_path) out_dir = f"seed-*/**/*-{args.temperature}" if args.top_p != 1.0: out_dir += f"-p{args.top_p}" if args.max_tokens != 512: out_dir += f"-max{args.max_tokens}" args.data_path = args.data_path / args.dataset / out_dir paths = list(sorted(glob(str(args.data_path), recursive=True))) def find_start(tokens, dataset="mbpp"): if dataset == "mbpp": match_token = ["<", "info", ">"] else: match_token = ["<", "text", ">"] for i in range(len(tokens) - 3, 0, -1): if tokens[i : i + 3] == match_token: break return i def batch_query_reverse_logp(all_codex_data, args, verbose=False): for outer_i, batch_start in enumerate( trange(0, len(all_codex_data), args.batch_size, disable=not verbose) ): batch_data = all_codex_data[batch_start : batch_start + args.batch_size] batch_prompts = [] batch_prompts_without_ref = [] for codex_data in batch_data: prompt = codex_data["prompt"] prompt_parse = parse_prompt(prompt, dataset=args.dataset) code_sample = codex_data["trg_prediction"] prompt_parse[-1]["code"] = f"<code>{code_sample}</code>" if args.dataset == "mbpp" and args.canonicalize: try: code_sample = clean_comment(code_sample) except: code_sample = code_sample code_sample = remove_print(code_sample) with_ref_prompt, without_ref_prompt = make_new_context( prompt_parse, dataset=args.dataset ) batch_prompts.append(with_ref_prompt) batch_prompts_without_ref.append(without_ref_prompt) with_ref_reponse, _ = safe_codex_call( args, batch_prompts, temperature=1.0, echo=True, max_tokens=0, api_i=outer_i % 3, ) for batch_i, (codex_data, with_ref_prompt, without_ref_prompt) in enumerate( zip(batch_data, batch_prompts, batch_prompts_without_ref) ): num_api_tokens = find_start( with_ref_reponse["choices"][batch_i]["logprobs"]["tokens"], dataset=args.dataset, ) gt_prompt_logprob = with_ref_reponse["choices"][batch_i]["logprobs"][ "token_logprobs" ][num_api_tokens:] gt_prompt_tokens = with_ref_reponse["choices"][batch_i]["logprobs"][ "tokens" ][num_api_tokens:] codex_data["reverse_prompt_with_ref"] = with_ref_prompt codex_data["reverse_prompt_without_ref"] = without_ref_prompt codex_data["prompt_reverse_tokens"] = gt_prompt_tokens codex_data["prompt_reverse_logprobs"] = gt_prompt_logprob codex_data["prompt_reverse_full_tokens"] = with_ref_reponse["choices"][ batch_i ]["logprobs"]["tokens"] codex_data["prompt_reverse_full_logprobs"] = with_ref_reponse["choices"][ batch_i ]["logprobs"]["token_logprobs"] return all_codex_data paths = sorted(paths) print(paths) for path in tqdm(paths, desc="total seeds", disable=False): path = Path(path) for sample_i in trange(args.num_samples, leave=False): if len(args.tag) == 0: output_file_name = f"{args.split}-{sample_i}-with-reverse.jsonl" else: output_file_name = f"{args.split}-{sample_i}-with-reverse-{args.tag}.jsonl" try: all_codex_data = [] with open(path / f"{args.split}-{sample_i}.jsonl", "r") as f: for i, line in enumerate(f): codex_data = json.loads(line) codex_data = json.loads(line) all_codex_data.append(codex_data) except Exception as e: print(e) print(f"{path / output_file_name} not ready yet. skipping.") continue if (path / output_file_name).exists(): with open(path / output_file_name, "r") as f: line_num = len(f.readlines()) if line_num == len(all_codex_data): continue from multiprocessing import Pool if args.num_procs > 1: all_codex_data_with_reverse = [] chunk_size = len(all_codex_data) // args.num_procs + 1 chunked_all_codex_data = [ all_codex_data[chunk_start : chunk_start + chunk_size] for chunk_start in range(0, len(all_codex_data), chunk_size) ] with Pool(processes=args.num_procs) as pool: for codex_data_with_reverse in tqdm( pool.imap( partial(batch_query_reverse_logp, args=args, verbose=True), chunked_all_codex_data, ), total=len(chunked_all_codex_data), ): all_codex_data_with_reverse.extend(codex_data_with_reverse) else: all_codex_data_with_reverse = batch_query_reverse_logp( all_codex_data, args, verbose=True ) with open(path / output_file_name, "w") as f: for codex_data_with_reverse in all_codex_data_with_reverse: codex_data_json = json.dumps(codex_data_with_reverse) f.write(codex_data_json + "\n")
coder_reviewer_reranking-main
fewshot_reviewer.py
# Copyright (c) Meta Platforms, Inc. and affiliates. import argparse import data from collectors import CollectorWithInfo if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--info-mode", type=str, default="assertion", choices=["function_name", "assertion"], ) parser.add_argument( "--dataset", type=str, choices=["mbpp_sanitized", "codet_humaneval", "humaneval"], ) parser.add_argument("--num_seeds", type=int, default=25) parser.add_argument("--num_samples", type=int, default=5) args = CollectorWithInfo.parse_args(parser) args.output_path = args.output_path + "/" + str(args.dataset) if args.dataset == "codet_humaneval": data_file_path = "dataset/human_eval/dataset/CodeTHumanEval.jsonl" elif args.dataset == "mbpp_sanitized": data_file_path = "dataset/mbpp/mbpp_sanitized_for_code_generation.jsonl" args.end_template = ["\nclass", "\ndef", "\n#", "\nif"] dataset = getattr(data, "HumanEvalDataset")(path=data_file_path, mode="prompt_only") collector = CollectorWithInfo.from_args(args, dataset) if args.temperature > 0: args.seed = list(range(args.num_seeds)) for i in range(args.num_samples): collector(i, i, 5) else: args.seed = list(range(args.num_seeds)) collector(0, 0, 1)
coder_reviewer_reranking-main
collect_zeroshot.py
# Copyright (c) Meta Platforms, Inc. and affiliates. import collections import json import os import regex class NL2BashDataset(object): def __init__(self, path="dataset/nl2bash/data/bash"): self.data = collections.defaultdict() for split in ["train", "dev", "test"]: nls = [x.strip() for x in open(os.path.join(path, f"{split}.nl.filtered"))] cms = [x.strip() for x in open(os.path.join(path, f"{split}.cm.filtered"))] infos = ["" for x in open(os.path.join(path, f"{split}.cm.filtered"))] self.data[split] = list(zip(nls, cms, infos)) class SpiderDataset(object): def __init__(self, path="dataset/spider"): self.data = collections.defaultdict() self.dbs = json.load(open(f"{path}/tables.json")) self.id2db = {item["db_id"]: item for item in self.dbs} for split in ["train", "dev"]: split_fname = "train_spider" if split == "train" else split data = json.load(open(f"{path}/{split_fname}.json")) nls = [x["question"] for x in data] cms = [x["query"] for x in data] db_info = [self.extract_db_info(x["db_id"]) for x in data] self.data[split] = list(zip(nls, cms, db_info)) def extract_db_info(self, db_id): db = self.id2db[db_id] id2table = { i: table_name for i, table_name in enumerate(db["table_names_original"]) } info = f"{db_id} " used_table_id = set() for table_id, column_name in db["column_names_original"]: if table_id == -1: info += f"| {column_name} " elif table_id not in used_table_id: info += f"| {id2table[table_id]} : {column_name} " used_table_id.add(table_id) else: info += f", {column_name} " return info.strip() class MBPPGoogleDataset(object): def __init__(self, path="dataset/mbpp/mbpp.jsonl", mode="function_name"): raw_data = sorted( [json.loads(x) for x in open(path)], key=lambda x: x["task_id"] ) for i, data_item in enumerate(raw_data): assert data_item["task_id"] == i + 1 self.raw_data = collections.defaultdict() self.mode = mode # 374 for training, 100 heldout, 500 test self.raw_data["train"] = raw_data[:10] + raw_data[510:] self.raw_data["test"] = raw_data[10:510] # data for codex collector, in input-output-info format self.data = collections.defaultdict() for split in self.raw_data: self.data[split] = self.extract_data(self.raw_data[split], mode) @staticmethod def extract_data(raw_data, mode): if mode == "function_name": get_function_name = lambda test_example: regex.match( "assert [\(]*([^\(]+)\(", test_example ).group(1) info = [get_function_name(x["test_list"][0]) for x in raw_data] elif mode == "assertion": info = [x["test_list"][0] for x in raw_data] elif mode == "assertion-full": info = [x["test_list"] for x in raw_data] else: raise Exception(f"Mode {mode} not supported.") nls = [x["text"] for x in raw_data] codes = [x["code"] for x in raw_data] return list(zip(nls, codes, info)) from dataset.human_eval.human_eval.data import read_problems class HumanEvalDataset(object): def __init__( self, path="dataset/human_eval/dataset/HumanEval.jsonl", assertion_path="", mode="assertion", ): self.path = path self.data = dict() self.raw_data = read_problems(path) self.mode = mode if assertion_path != "": self.assertion_data = read_problems(assertion_path) else: self.assertion_data = self.raw_data self.data["test"] = self.extract_data() def extract_data(self): nls = [] codes = [] info = [] for pid, prob in self.raw_data.items(): assert_prob = self.assertion_data[pid] nls.append(prob["prompt"]) docstring, func_header, func_context, doc_start = extract_docstring( assert_prob["prompt"] ) self.raw_data[pid]["func_header"] = func_header.strip() + "\n" self.raw_data[pid]["func_context"] = func_context codes.append(prob["canonical_solution"]) if self.mode != "prompt_only": assertions = extract_test(pid, prob["entry_point"], docstring) if self.mode == "assertion": self.raw_data[pid]["assertion"] = assertions[0] info.append(assertions[0]) else: self.raw_data[pid]["assertion"] = assertions info.append(assertions) else: info.append([]) return list(zip(nls, codes, info)) class MBPPSanDataset(HumanEvalDataset): def extract_data(self): nls = [] codes = [] info = [] for pid, prob in self.raw_data.items(): nls.append(prob["prompt"]) docstring, func_header, func_context, doc_start = extract_docstring( prob["prompt"] ) self.raw_data[pid]["func_header"] = func_header.strip() + "\n" self.raw_data[pid]["func_context"] = func_context codes.append(prob["canonical_solution"]) if self.mode != "prompt_only": assertions = [ l.strip() for l in prob["test"].split("\n")[1:] if l.strip() != "" ] if self.mode == "assertion": self.raw_data[pid]["assertion"] = assertions[0] info.append(assertions[0]) elif self.mode == "assertion-all": self.raw_data[pid]["assertion"] = assertions info.append(assertions) else: raise ValueError("invalid mode") else: info.append([]) return list(zip(nls, codes, info)) def rindex(lst, value): return len(lst) - lst[::-1].index(value) - 1 def _check_test_case_validation(test_case): if len(test_case.strip()) < 1: return False if "assert" not in test_case: return False try: multi_line_test_case = test_case.replace("\n", "\n ") assert_in_a_block = f"try:\n {multi_line_test_case}\nexcept:\n pass\n" compile(assert_in_a_block, "", "exec") return True except Exception: return False def extract_generated_tests(content, entry_point): def _truncate(content): for identifier in ["\nclass", "\ndef", "\n#", "\nif", "\nprint"]: if identifier in content: content = content.split(identifier)[0] return content.strip() split_by_assert = [ f"assert {part}".strip() for part in f"assert {content}".split("assert ") if (entry_point.strip() in part) and len(part.strip()) > 0 ] truncated_test_cases = [_truncate(i) for i in split_by_assert] checked_assertions = [ i for i in truncated_test_cases if _check_test_case_validation(i) ] return checked_assertions def extract_docstring(prompt): func_start = max(rindex(prompt, " fed") - 4, 0) clean_prompt = prompt[func_start:] if '"""' in prompt: doc_start = '"""' else: doc_start = "'''" docstring = clean_prompt[clean_prompt.strip().index(doc_start) :] func_header = clean_prompt[: clean_prompt.strip().index(doc_start)] func_context = prompt[:func_start] return docstring, func_header, func_context, doc_start def extract_test(pid, func_name, docstring): if pid in manual_extract: return manual_extract[pid] else: return _extract_tests(func_name, docstring) def _extract_tests(func_name, docstring): all_tests = [] doc_lines = docstring.strip().split("\n") test_start = False if ">>>" in docstring: for l in doc_lines: if not test_start: if ">>>" in l and func_name in l: test_start = True if test_start: if ">>>" in l and func_name in l: l = l.strip()[3:].strip() all_tests.append(l) elif l.strip() != "" and '"""' not in l: all_tests[-1] = "assert " + all_tests[-1] + f" == {l.strip()}" test_start = False elif any( ["==>" in docstring, "=>" in docstring, "->" in docstring, "➞" in docstring] ): for special_char in ["==>", "=>", "->", "➞", "==>"]: if special_char in docstring: break for l in doc_lines: if not test_start: if special_char in l and func_name in l: test_start = True if test_start and (special_char in l and func_name in l): l = l.strip().replace(special_char, "==") l = "assert " + l all_tests.append(l) elif any(["==" in docstring, "returns" in docstring]): for special_char in ["==", "returns"]: if special_char in docstring: break for l in doc_lines: if not test_start: if special_char in l and func_name + "(" in l: test_start = True if test_start and (special_char in l and func_name in l): l = "assert " + l.strip().replace(special_char, "==") all_tests.append(l) return all_tests manual_extract = { "HumanEval/12": [ "assert longest(['a', 'b', 'c']) == 'a'", "assert longest(['a', 'bb', 'ccc']) == 'ccc'", ], "HumanEval/38": ["assert True == True"], # empty assertion to handle no doc test "HumanEval/41": ["assert True == True"], # empty assertion to handle no doc test "HumanEval/50": ["assert True == True"], # empty assertion to handle no doc test "HumanEval/67": [ 'assert fruit_distribution("5 apples and 6 oranges", 19) == 8' 'assert fruit_distribution("0 apples and 1 oranges",3) == 2' 'assert fruit_distribution("2 apples and 3 oranges", 100) == 95' 'assert fruit_distribution("100 apples and 1 oranges",120) == 19' ], "HumanEval/68": [ "assert pluck([4,2,3]) == [2, 1]", "assert pluck([1,2,3]) == [2, 1]", "assert pluck([]) == []", "assert pluck([5, 0, 3, 0, 4, 2]) == [0, 1]", ], "HumanEval/78": [ "assert hex_key('AB') == 1", "assert hex_key('1077E') == 2", "assert hex_key('ABED1A33') == 4", "assert hex_key('123456789ABCDEF0') == 6", "assert hex_key('2020') == 2", ], "HumanEval/79": [ "assert decimal_to_binary(15) == 'db1111db'", "assert decimal_to_binary(32) == 'db100000db'", ], "HumanEval/81": [ "assert grade_equation([4.0, 3, 1.7, 2, 3.5]) ==> ['A+', 'B', 'C-', 'C', 'A-']" ], "HumanEval/83": ["assert True == True"], # empty assertion to handle no doc test "HumanEval/84": ["assert True == True"], # empty assertion to handle no doc test "HumanEval/86": [ "assert anti_shuffle('Hi') == 'Hi'", "assert anti_shuffle('hello') == 'ehllo'", "assert anti_shuffle('Hello World!!!') == 'Hello !!!Wdlor'", ], "HumanEval/88": [ "assert sort_array([]) == []", "assert sort_array([5]) == [5]", "assert sort_array([2, 4, 3, 0, 1, 5]) == [0, 1, 2, 3, 4, 5]", "assert sort_array([2, 4, 3, 0, 1, 5, 6]) == [6, 5, 4, 3, 2, 1, 0]", ], "HumanEval/94": [ "assert skjkasdkd([0,3,2,1,3,5,7,4,5,5,5,2,181,32,4,32,3,2,32,324,4,3]) == 10", "assert skjkasdkd([1,0,1,8,2,4597,2,1,3,40,1,2,1,2,4,2,5,1]) == 25", "assert skjkasdkd([1,3,1,32,5107,34,83278,109,163,23,2323,32,30,1,9,3]) == 13", "assert skjkasdkd([0,724,32,71,99,32,6,0,5,91,83,0,5,6]) == 11", "assert skjkasdkd([0,81,12,3,1,21]) == 3", "assert skjkasdkd([0,8,1,2,1,7]) == 7", ], "HumanEval/95": [ 'assert check_dict_case({"a":"apple", "b":"banana"}) == True.', 'assert check_dict_case({"a":"apple", "A":"banana", "B":"banana"}) == False.', 'assert check_dict_case({"a":"apple", 8:"banana", "a":"apple"}) == False.', 'assert check_dict_case({"Name":"John", "Age":"36", "City":"Houston"}) == False.', 'assert check_dict_case({"STATE":"NC", "ZIP":"12345" }) == True.', ], "HumanEval/97": [ "assert multiply(148, 412) == 16", "assert multiply(19, 28) == 72", "assert multiply(2020, 1851) == 0", "assert multiply(14,-15) == 20", ], "HumanEval/102": [ "assert choose_num(12, 15) == 14", "assert choose_num(13, 12) == -1", ], "HumanEval/105": ["assert True == True"], "HumanEval/107": [ "assert even_odd_palindrome(3) == (1, 3)", "assert even_odd_palindrome(12) == (4, 6)", ], "HumanEval/108": [ "assert count_nums([]) == 0", "assert count_nums([-1, 11, -11]) == 1", "assert count_nums([1, 1, 2]) == 3", ], "HumanEval/115": [ "assert max_fill([[0,0,1,0], [0,1,0,0], [1,1,1,1]]) == 1", "assert max_fill([[0,0,1,1], [0,0,0,0], [1,1,1,1], [0,1,1,1]]) == 2", "assert max_fill([[0,0,0], [0,0,0]]) == 0", ], "HumanEval/116": [ "assert sort_array([1, 5, 2, 3, 4]) == [1, 2, 3, 4, 5]", "assert sort_array([-2, -3, -4, -5, -6]) == [-6, -5, -4, -3, -2]", "assert sort_array([1, 0, 2, 3, 4]) == [0, 1, 2, 3, 4]", ], "HumanEval/112": [ "assert reverse_delete('abcde', 'ae') == ('bcd',False)", "assert reverse_delete('abcdef', 'b') == ('acdef',False)", "assert reverse_delete('abcdedcba', 'ab') == ('cdedc',True)", ], "HumanEval/120": [ "assert maximum([-3, -4, 5], 3) == [-4, -3, 5]", "assert maximum([4, -4, 4], 2) == [4, 4]", "assert maximum([-3, 2, 1, 2, -1, -2, 1], 1) == [2]", ], "HumanEval/122": [ "assert add_elements([111,21,3,4000,5,6,7,8,9]) == 24", ], "HumanEval/128": [ "assert prod_signs([1, 2, 2, -4]) == -9", "assert prod_signs([0, 1]) == 0", "assert prod_signs([]) == None", ], "HumanEval/129": [ "assert minPath([[1,2,3], [4,5,6], [7,8,9]], 3) == [1, 2, 1]", "assert minPath([[5,9,3], [4,1,6], [7,8,2]], 1) == [1]", ], "HumanEval/130": ["assert tri(3) == [1, 3, 2, 8]"], "HumanEval/133": [ "assert sum_squares([1,2,3]) == 14", "assert sum_squares([1,4,9]) == 98", "assert sum_squares([1,3,5,7]) == 84", "assert sum_squares([1.4,4.2,0]) == 29", "assert sum_squares([-2.4,1,1]) == 6", ], "HumanEval/135": [ "assert can_arrange([1,2,4,3,5]) == 3", "assert can_arrange([1,2,3]) == -1", ], "HumanEval/141": [ "assert file_name_check('example.txt') == 'Yes'", "assert file_name_check('1example.dll') == 'No'", ], "HumanEval/142": [ "assert sum_squares([1,2,3]) == 6", "assert sum_squares([]) == 0", "assert sum_squares([-1,-5,2,-1,-5]) == -126", ], "HumanEval/143": [ "assert words_in_sentence('This is a test') == 'is'", "assert words_in_sentence('lets go for swimming') == 'go for'", ], "HumanEval/144": [ 'assert simplify("1/5", "5/1") == True', 'assert simplify("1/6", "2/1") == False', 'assert simplify("7/10", "10/2") == False', ], "HumanEval/145": [ "assert order_by_points([1, 11, -1, -11, -12]) == [-1, -11, 1, -12, 11]", "assert order_by_points([]) == []", ], "HumanEval/156": [ "assert int_to_mini_roman(19) == 'xix'", "assert int_to_mini_roman(152) == 'clii'", "assert int_to_mini_roman(426) == 'cdxxvi'", ], "HumanEval/147": [ "assert get_max_triples(5) == 1", ], "HumanEval/149": [ 'assert list_sort(["aa", "a", "aaa"]) == ["aa"]', 'assert list_sort(["ab", "a", "aaa", "cd"]) == ["ab", "cd"]', ], "HumanEval/159": [ "assert eat(5, 6, 10) == [11, 4]", "assert eat(4, 8, 9) == [12, 1]", "assert eat(1, 10, 10) == [11, 0]", "assert eat(2, 11, 5) == [7, 0]", ], "HumanEval/160": [ "assert do_algebra([2, 3, 4, 5], ['+', '*', '-']) == 9", ], "HumanEval/161": [ 'assert solve("1234") == "4321"', 'assert solve("ab") == "AB"', 'assert solve("#a@C") == "#A@c"', ], "HumanEval/162": [ "assert string_to_md5('Hello world') == '3e25960a79dbc69b674cd4ec67a72c62'" ], }
coder_reviewer_reranking-main
data.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from torchvision import datasets, transforms from torch.utils.data.sampler import RandomSampler import torchvision import torchvision.transforms as transforms import torch.utils.data import pdb class CIFAR10_Wrapper(torch.utils.data.Dataset): def __init__(self, root, train, download, transform): self.dataset = torchvision.datasets.CIFAR10( root=root, train=train, download=download, transform=transform) self.transformed_cache = {} self.access_total = 0 self.cache_hit = 0 self.access_since_retransform = 0 def __getitem__(self, index): #print(self.transformed_cache.keys()) if index in self.transformed_cache.keys(): item = self.transformed_cache[index] self.cache_hit += 1 #print("Using cache: ", index) else: item = self.dataset[index] self.transformed_cache[index] = item #pdb.set_trace() #print("Writing cache: ", index) self.access_total += 1 self.access_since_retransform += 1 #print("since retransform: ", self.access_since_retransform) #print("total: ", self.access_total) return item def __len__(self): return len(self.dataset) #return 128 # flushes the cache of transformed images def retransform(self): print("total calls retransform: {}, cache hits: {}".format( self.access_since_retransform, self.cache_hit)) #print("total: ", self.access_total) self.transformed_cache = {} self.access_since_retransform = 0 self.cache_hit = 0
deep-variance-reduction-main
cifar_wrapper.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. #from __future__ import print_function import argparse import pickle import os import time from timeit import default_timer as timer import torch import torch.nn as nn from torch.autograd import Variable import sys import math import problems import optimizers import logging import pdb from torch.nn.functional import nll_loss, log_softmax import numpy as np from diagnostics import * from run_vr import * import torch.backends.cudnn as cudnn def run(args_override={}): run_dir = "runs" disable_cuda = False checkpoint_dir = "/checkpoint/{}/checkpoints".format(os.environ["USER"]) default_momentum = 0.9 default_lr = 0.1 default_decay = 0.0001 default_epochs = 300 default_batch_size = 128 default_tail_average = 0.0 default_tail_average_all = False default_half_precision = False default_method = "sgd" #"svrg" #"sgd" default_log_diagnostics = False default_log_diagnostics_every_epoch = False default_log_fast_diagnostics = False default_logfname = "log" default_log_interval = 20 default_transform_locking = True default_per_block = False default_dropout = False default_batchnorm = True default_vr_from_epoch = 1 # 1 is first epoch. default_calculate_train_loss_each_epoch = False default_save_model = False # Saving every 10 epochs default_resume = False default_resume_from = "" # It will always resume from a checkpoint default_full_checkpointing = False default_second_lambda = 0.5 default_inner_steps = 10 default_clamping = 1000.0 default_vr_bn_at_recalibration = True default_variance_reg = 0.01 default_lr_reduction = "150-225" default_L = 1.0 default_architecture = "default" default_problem = "cifar10" # Training settings parser = argparse.ArgumentParser(description='PyTorch optimization testbed') parser.add_argument('--problem', type=str, default=default_problem, help='Problem instance (default: ' + default_problem + ')') parser.add_argument('--method', type=str, default=default_method, help='Optimization method (default: ' + default_method + ')') parser.add_argument('--batch-size', type=int, default=default_batch_size, metavar='M', help='minibatch size (default: ' + str(default_batch_size) + ')') parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N', help='input batch size for testing (default: 1000)') parser.add_argument('--epochs', type=int, default=default_epochs, metavar='N', help='number of epochs to train (default: ' + str(default_epochs) + ')') parser.add_argument('--lr', type=float, default=default_lr, metavar='LR', help='learning rate (default: ' + str(default_lr) + ')') parser.add_argument('--momentum', type=float, default=default_momentum, metavar='M', help='SGD momentum (default: ' + str(default_momentum) + ')') parser.add_argument('--decay', type=float, default=default_decay, metavar='M', help='SGD weight decay (default: ' + str(default_decay) + ')') parser.add_argument('--L', type=float, default=default_L, metavar='L', help='SGD L estimate (default: ' + str(default_L) + ')') parser.add_argument('--tail_average', type=float, default=default_tail_average, help='Use tail averaging of iterates every epoch, with the given tail fraction (default: ' + str(default_tail_average) + ')') parser.add_argument('--tail_average_all', type=str2bool, default=default_tail_average_all, help='Apply tail aveaging either to the whole run or just after the first lr reduction (default: ' + str(default_tail_average_all) + ')') parser.add_argument('--clamping', type=float, default=default_clamping, metavar='C', help='APS clamping (default: ' + str(default_clamping) + ')') parser.add_argument('--inner_steps', type=int, default=default_inner_steps, metavar='N', help='Inner steps for implicit methods (default: ' + str(default_inner_steps) + ')') parser.add_argument('--vr_from_epoch', type=int, default=default_vr_from_epoch, help='Start VR (if in use) at this epoch (default: ' + str(default_vr_from_epoch) + ')') parser.add_argument('--no-cuda', action='store_true', default=disable_cuda, help='disables CUDA training') parser.add_argument('--half_precision', type=str2bool, default=default_half_precision, help='Use half precision (default: ' + str(default_half_precision) + ')') parser.add_argument('--second_lambda', type=float, default=default_second_lambda, metavar='D', help='A second linear interpolation factor used by some algorithms (default: ' + str(default_second_lambda) + ')') parser.add_argument('--variance_reg', type=float, default=default_variance_reg, metavar='D', help='Added to the variance in reparam to prevent divide by 0 problems (default: ' + str(default_variance_reg) + ')') parser.add_argument('--architecture', type=str, default=default_architecture, help='architecture (default: ' + default_architecture + ')') parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)') parser.add_argument('--dropout', type=str2bool, default=default_dropout, help='Use dropout (default: ' + str(default_dropout) + ')') parser.add_argument('--batchnorm', type=str2bool, default=default_batchnorm, help='Use batchnorm (default: ' + str(default_batchnorm) + ')') parser.add_argument('--transform_locking', type=str2bool, default=default_transform_locking, help='Transform locking: ' + str(default_transform_locking) + ')') parser.add_argument('--log_diagnostics', type=str2bool, default=default_log_diagnostics, help='produce and log expensive diagnostics (default: ' + str(default_log_diagnostics) + ')') parser.add_argument('--log_diagnostics_every_epoch', type=str2bool, default=default_log_diagnostics_every_epoch, help='do full diagnostics every epoch instead of every 10') parser.add_argument('--log_diagnostics_deciles', type=str2bool, default=False, help='full diagnostics at every 10% of the epoch') parser.add_argument('--log_fast_diagnostics', type=str2bool, default=default_log_fast_diagnostics, help='produce and log cheap diagnostics (default: ' + str(default_log_fast_diagnostics) + ')') parser.add_argument('--logfname', type=str, default=default_logfname, help='Prefix for diagonstic log files (default: ' + str(default_logfname) + ')') parser.add_argument('--save_model', type=str2bool, default=default_save_model, help='Save model every 10 epochs (default: ' + str(default_save_model) + ')') parser.add_argument('--resume', type=str2bool, default=default_resume, help='Resume from resume_from (default: ' + str(default_resume) + ')') parser.add_argument('--resume_from', type=str, default=default_resume_from, help=' Path to saved model (default: ' + str(default_resume_from) + ')') parser.add_argument('--full_checkpointing', type=str2bool, default=default_full_checkpointing, help='Writeout and resume from checkpoints (default: ' + str(default_full_checkpointing) + ')') parser.add_argument('--calculate_train_loss_each_epoch', type=str, default=default_calculate_train_loss_each_epoch, help=' Do a 2nd pass after each epoch to calculate the training error rate/loss (default: ' + str(default_calculate_train_loss_each_epoch) + ')') parser.add_argument('--vr_bn_at_recalibration', type=str2bool, default=default_vr_bn_at_recalibration, help='Use batch norm on the recalibration pass (default: ' + str(default_vr_bn_at_recalibration) + ')') parser.add_argument('--lr_reduction', type=str, default=default_lr_reduction, help='Use lr reduction specified (default: ' + str(default_lr_reduction) + ')') parser.add_argument('--log_interval', type=int, default=default_log_interval, metavar='N', help='how many batches to wait before logging training status') parser.add_argument('--per_block', type=str2bool, default=default_per_block, help='Use per block learning rates (default: ' + str(default_per_block) + ')') args = parser.parse_args([]) # Don't actually use command line arguments, put from call to function only # Apply overrides? args.__dict__.update(args_override) if isinstance(args, dict): args = Struct(**args) #"scsg" args.opt_vr = opt_vr = (args.method in ["saga", "svrg", "pointsaga", "recompute_svrg", "online_svrg"]) run_name = (args.problem + "-" + args.architecture + "-" + args.method + "-lr" + str(args.lr) + "-m" + str(args.momentum) + "-" + "d" + str(args.decay) + "-epochs" + str(args.epochs) + "bs" + str(args.batch_size) + "reduct_" + args.lr_reduction) if not args.batchnorm: run_name += "_nobn" if args.dropout: run_name += "_dropout" if args.opt_vr and args.vr_from_epoch != 1: run_name += "_vr_from_" + str(args.vr_from_epoch) if not args.vr_bn_at_recalibration: run_name += "_bn_recal_" + str(args.vr_bn_at_recalibration) if args.resume: run_name += "_resume" if args.seed != 1: run_name += "seed_" + str(args.seed) if args.half_precision: run_name += "_half" if args.tail_average > 0: run_name += "_tavg_" + str(args.tail_average) if args.tail_average_all: run_name += "_tall" run_name = run_name.strip().replace('.', '_') # SETUP LOGGING root = logging.getLogger() root.setLevel(logging.INFO) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s | %(message)s') ch.setFormatter(formatter) #if 'ch' in locals(): root.addHandler(ch) ############ logging.info("Run " + run_name) logging.info("#########") logging.info(args) args.cuda = not args.no_cuda and torch.cuda.is_available() logging.info("Using CUDA: {} CUDA AVAIL: {} #DEVICES: {}".format( args.cuda, torch.cuda.is_available(), torch.cuda.device_count())) cudnn.benchmark = True logging.info("Loading data") train_loader, test_loader, model, train_dataset = problems.load(args) if hasattr(model, "sampler") and hasattr(model.sampler, "reorder"): logging.info("NOTE: Consistant batch sampling in use") if args.cuda: logging.info("model.cuda") model.cuda() logging.info("") if args.half_precision: logging.info("Using half precision") model = model.half() if args.resume: # Load model.load_state_dict(torch.load(args.resume_from, map_location=lambda storage, loc: storage)) model.cuda() logging.info("Resuming from file: {}".format(args.resume_from)) checkpoint_resume = False if args.full_checkpointing: # Look for and load checkpoint model checkpoint_model_path = checkpoint_dir + "/" + run_name + "_checkpoint.model" checkpoint_runinfo_path = checkpoint_dir + "/" + run_name + "_info.pkl" if os.path.exists(checkpoint_model_path): checkpoint_resume = True logging.info("Checkpoint found: {}".format(checkpoint_model_path)) model.load_state_dict(torch.load(checkpoint_model_path, map_location=lambda storage, loc: storage)) model.cuda() with open(checkpoint_runinfo_path, 'rb') as fcheckpoint: runinfo = pickle.load(fcheckpoint) if runinfo["epoch"] >= args.epochs: logging.error("runinfo['epoch']: {} >= args.epochs, checkpoint is past/at end of run".format(runinfo["epoch"])) return else: logging.info("No checkpoint exists, starting a fresh run") ############################ # logging.info some information about the model logging.info("Model statistics:") nparams = 0 group_idx = 0 for param in model.parameters(): #import pdb; pdb.set_trace() group_size = 1 for g in param.size(): group_size *= g nparams += group_size group_idx += 1 train_nbatches = len(train_loader) logging.info("total parameters: {:,}".format(nparams)) logging.info("minibatch size: {}".format(args.batch_size)) logging.info("Rough training dataset size: {:,} number of minibatches: {}".format( len(train_loader)*args.batch_size, train_nbatches)) logging.info("Rough test dataset size: {:,} number of test minibatches: {}".format( len(test_loader)*args.batch_size, len(test_loader))) # Averaging fraction calculation ntail_batches = int(train_nbatches*args.tail_average) if ntail_batches == 0: ntail_batches = 1 ntail_from = train_nbatches - ntail_batches logging.info("Tail averaging fraction {:.2f} will average {} batches, from batch: {}, tail_average_all: {}".format( args.tail_average, ntail_batches, ntail_from, args.tail_average_all )) logging.info("Creating optimizer") optimizer = optimizers.optimizer(model, args) criterion = nn.CrossEntropyLoss() def train(epoch): model.train() interval = timer() start = timer() start_time = time.time() time_cuda = 0.0 time_variable = 0.0 time_forward = 0.0 time_backward = 0.0 time_step = 0.0 time_load = 0.0 if args.tail_average > 0.0: averaged_so_far = 0 # create/zero tail_average storage for group in optimizer.param_groups: for p in group['params']: param_state = optimizer.state[p] if 'tail_average' not in param_state: param_state['tail_average'] = p.data.clone().double().zero_() load_start_time = time.time() for batch_idx, (data, target) in enumerate(train_loader): time_load += time.time() - load_start_time cuda_time = time.time() if args.cuda: data, target = data.cuda(), target.cuda(non_blocking=True) if args.half_precision: data = data.half() variable_time = time.time() time_cuda += variable_time - cuda_time data, target = Variable(data), Variable(target) time_variable += time.time() - variable_time def eval_closure(): nonlocal time_forward nonlocal time_backward closure_time = time.time() optimizer.zero_grad() output = model(data) loss = criterion(output, target) eval_time = time.time() time_forward += eval_time - closure_time loss.backward() time_backward += time.time() - eval_time return loss step_start_time = time.time() if hasattr(optimizer, "step_preds"): def partial_closure(): optimizer.zero_grad() output = model(data) logprobs = log_softmax(output) return logprobs loss = optimizer.step_preds(partial_closure, target) elif opt_vr: loss = optimizer.step(batch_idx, closure=eval_closure) else: loss = optimizer.step(closure=eval_closure) time_step += time.time() - step_start_time if args.log_diagnostics and epoch >= args.vr_from_epoch: if args.method == "svrg": in_run_diagnostics(epoch, batch_idx, args, train_loader, optimizer, model, criterion) # Accumulate tail average if args.tail_average > 0.0: if batch_idx >= ntail_from: averaged_so_far += 1 for group in optimizer.param_groups: for p in group['params']: param_state = optimizer.state[p] tail = param_state['tail_average'] # Running mean calculation tail.add_(1.0/averaged_so_far, p.data.double() - tail) if batch_idx % args.log_interval == 0: mid = timer() percent_done = 100. * batch_idx / len(train_loader) if percent_done > 0: time_estimate = math.ceil((mid - start)*(100/percent_done)) time_estimate = str(datetime.timedelta(seconds=time_estimate)) inst_estimate = math.ceil((mid - interval)*(len(train_loader)/args.log_interval)) inst_estimate = str(datetime.timedelta(seconds=inst_estimate)) else: time_estimate = "unknown" inst_estimate = "unknown" logging.info('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}, time: {} inst: {}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.data.item(), time_estimate, inst_estimate)) if False: since_start = time.time() logging.info("load: {:.3f}, cuda: {:.3f}, variable: {:.3f}, forward: {:.3f}, backward: {:.3f}, step: {:.3f}, step-clo: {:.3f}, sum: {}, actual: {}".format( time_load, time_cuda, time_variable, time_forward, time_backward, time_step, time_step - time_forward - time_backward, time_load + time_cuda + time_variable + time_step, since_start - start_time )) time_cuda = 0.0 time_variable = 0.0 time_forward = 0.0 time_backward = 0.0 time_step = 0.0 time_load = 0.0 interval = timer() load_start_time = time.time() if args.tail_average > 0.0: if averaged_so_far != ntail_batches: raise Exception("Off by one: {}, {}".format(averaged_so_far, ntail_batches)) current_lr = optimizer.param_groups[0]['lr'] if args.tail_average_all or args.lr != current_lr: logging.info("Setting x to tail average ({}), current_lr: {}".format( args.tail_average, current_lr)) for group in optimizer.param_groups: for p in group['params']: param_state = optimizer.state[p] tail = param_state['tail_average'] p.data.zero_().add_(tail.type_as(p.data)) if hasattr(model, "sampler") and hasattr(model.sampler, "reorder"): model.sampler.reorder() if hasattr(train_dataset, "retransform"): logging.info("retransform") train_dataset.retransform() def loss_stats(epoch, loader, setname): model.eval() loss = 0.0 correct = 0.0 for data, target in loader: if args.cuda: data, target = data.cuda(), target.cuda() if args.half_precision: data = data.half() data, target = Variable(data, volatile=True), Variable(target) output = model(data) loss += criterion(output, target).data.item() pred = output.data.max(1)[1] # index of the max log-probability correct += pred.eq(target.data).cpu().sum().float().item() loss /= len(loader) # loss function already averages over batch size error_rate = 100.0 * correct / len(loader.dataset) #pdb.set_trace() logging.info('{} set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)'.format( setname, loss, correct, len(loader.dataset), error_rate)) return (loss, error_rate) # Crate directory for saving model if needed problem_dir = run_dir + "/" + args.problem if not os.path.exists(run_dir): os.makedirs(run_dir) if not os.path.exists(problem_dir): os.makedirs(problem_dir) if not checkpoint_resume: runinfo = vars(args) runinfo["train_losses"] = [] runinfo["train_errors"] = [] runinfo["test_losses"] = [] runinfo["test_errors"] = [] runinfo["nparams"] = nparams runinfo["ndatapoints"] = len(train_loader)*args.batch_size runinfo["nminibatches"] = len(train_loader) runinfo["epoch"] = 0 else: # When resuming if hasattr(optimizer, "recalibrate"): logging.info("Recalibrate for restart, epoch: {}".format(runinfo["epoch"])) seed = runinfo["seed"] + 1031*runinfo["epoch"] torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) recalibrate(runinfo["epoch"], args, train_loader, test_loader, model, train_dataset, optimizer, criterion) for epoch in range(runinfo["epoch"]+1, args.epochs + 1): runinfo["epoch"] = epoch logging.info("Starting epoch {}".format(epoch)) seed = runinfo["seed"] + 1031*epoch torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) if epoch == 1 and hasattr(optimizer, "recalibrate"): recalibrate(epoch, args, train_loader, test_loader, model, train_dataset, optimizer, criterion) if args.lr_reduction == "default": lr = args.lr * (0.1 ** (epoch // 75)) elif args.lr_reduction == "none" or args.lr_reduction == "False": lr = args.lr elif args.lr_reduction == "150": lr = args.lr * (0.1 ** (epoch // 150)) elif args.lr_reduction == "150-225": lr = args.lr * (0.1 ** (epoch // 150)) * (0.1 ** (epoch // 225)) elif args.lr_reduction == "up5x-20-down150": if epoch < 20: lr = args.lr else: lr = 3.0 * args.lr * (0.1 ** (epoch // 150)) elif args.lr_reduction == "up30-150-225": if epoch < 30: lr = args.lr else: lr = 3.0 * args.lr * (0.1 ** (epoch // 150)) * (0.1 ** (epoch // 225)) elif args.lr_reduction == "every30": lr = args.lr * (0.1 ** (epoch // 30)) else: logging.info("Lr scheme not recognised: {}".format(args.lr_reduction)) for param_group in optimizer.param_groups: param_group['lr'] = lr logging.info("Learning rate: {}".format(lr)) start = timer() if args.method == "scsg": train_scsg(epoch, args, train_loader, test_loader, model, train_dataset, optimizer, criterion) else: train(epoch) end = timer() logging.info("Epoch took: {}".format(end-start)) logging.info("") if args.calculate_train_loss_each_epoch: (train_loss, train_err) = loss_stats(epoch, train_loader, "Train") #test(epoch) else: train_loss = 0 train_err = 0 runinfo["train_losses"].append(train_loss) runinfo["train_errors"].append(train_err) (test_loss, test_err) = loss_stats(epoch, test_loader, "Test") #test(epoch) runinfo["test_losses"].append(test_loss) runinfo["test_errors"].append(test_err) logging.info("") if args.log_fast_diagnostics and hasattr(optimizer, "store_old_table"): logging.info("Storing old table") optimizer.store_old_table() if hasattr(optimizer, "recalibrate"): recalibrate(epoch+1, args, train_loader, test_loader, model, train_dataset, optimizer, criterion) if False: # Only works for recompute_svrg I think batchnorm_diagnostics(epoch, args, train_loader, optimizer, model) if epoch >= args.vr_from_epoch and args.log_fast_diagnostics and hasattr(optimizer, "epoch_diagnostics"): optimizer.epoch_diagnostics(train_loss, train_err, test_loss, test_err) # Ocassionally save out the model. if args.save_model and epoch % 10 == 0: logging.info("Saving model ...") model_dir = problem_dir + "/model_" + run_name if not os.path.exists(model_dir): os.makedirs(model_dir) model_fname = "{}/epoch_{}.model".format(model_dir, epoch) torch.save(model.state_dict(), model_fname) logging.info("Saved model {}".format(model_fname)) out_fname = problem_dir + "/" + run_name + '_partial.pkl' with open(out_fname, 'wb') as output: pickle.dump(runinfo, output) print("Saved partial: {}".format(out_fname)) if args.full_checkpointing: checkpoint_model_path_tmp = checkpoint_model_path + ".tmp" logging.info("Saving checkpoint model ...") torch.save(model.state_dict(), checkpoint_model_path_tmp) os.rename(checkpoint_model_path_tmp, checkpoint_model_path) logging.info("Saved {}".format(checkpoint_model_path)) checkpoint_runinfo_path_tmp = checkpoint_runinfo_path + ".tmp" with open(checkpoint_runinfo_path_tmp, 'wb') as output: pickle.dump(runinfo, output) os.rename(checkpoint_runinfo_path_tmp, checkpoint_runinfo_path) print("Saved runinfo: {}".format(checkpoint_runinfo_path)) if True: if args.method == "reparm": optimizer.print_diagnostics() out_fname = problem_dir + "/" + run_name + '_final.pkl' with open(out_fname, 'wb') as output: pickle.dump(runinfo, output) print("Saved {}".format(out_fname)) if __name__ == "__main__": run()
deep-variance-reduction-main
run.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from torch.optim.optimizer import Optimizer, required import torch import pdb import pickle import math import logging class SCSG(Optimizer): def __init__(self, params, args, nbatches, model, vr_bn_at_recalibration, vr_from_epoch, lr=required, momentum=required, weight_decay=required): self.nbatches = nbatches self.batches_processed = 0 self.epoch = 0 self.vr_bn_at_recalibration = vr_bn_at_recalibration self.vr_from_epoch = vr_from_epoch self.model = model defaults = dict(lr=lr, momentum=momentum, weight_decay=weight_decay) self.recompute_version = False self.megabatch_size = 10 # number of minibatches in a megabatch self.recalibration_interval = 10 self.recalibration_i = 0 self.interval_i = 0 self.stats_buffered = False if self.megabatch_size != self.recalibration_interval: raise Exception("megabatch_size != recalibration_interval not supported yet") self.test_name = args.logfname self.running_tmp = {} self.running_interp = 0.9 super(SCSG, self).__init__(params, defaults) def __setstate__(self, state): super(SCSG, self).__setstate__(state) def initialize(self): for group in self.param_groups: for p in group['params']: momentum = group['momentum'] param_state = self.state[p] if 'gavg' not in param_state: param_state['gavg'] = p.data.clone().zero_() param_state['gavg_debug'] = p.data.clone().zero_() param_state['full_grad'] = p.data.clone().zero_() param_state['gi'] = p.data.clone().zero_() if not self.recompute_version: gsize = p.data.size() gtbl_size = torch.Size([self.megabatch_size] + list(gsize)) param_state['gktbl'] = torch.zeros(gtbl_size).cuda() # m2 is the running gradient variance accumulator param_state['m2'] = p.data.clone().zero_() param_state['grad_running_cov'] = p.data.clone().zero_() param_state['grad_running_var'] = p.data.clone().zero_() param_state['grad_running_mean'] = p.data.clone().zero_() if momentum != 0: if 'momentum_buffer' not in param_state: buf = param_state['momentum_buffer'] = p.data.clone().zero_() if 'tilde_x' not in param_state: param_state['tilde_x'] = p.data.clone() param_state['xk'] = p.data.clone() state = self.model.state_dict() # Batch norm's activation running_mean/var variables for skey in state.keys(): if skey.endswith(".running_mean") or skey.endswith(".running_var"): self.running_tmp[skey] = state[skey].clone() logging.info("running: {}".format(self.running_tmp.keys())) def recalibrate_start(self): """ Part of the recalibration pass with SVRG. Stores the gradients for later use. We don't do anything in online_svrg """ print("Recalibrate_start called") self.epoch += 1 self.initialize() gi_var = [0.0 for i in range(self.recalibration_interval)] vr_var = [0.0 for i in range(self.recalibration_interval)] if self.stats_buffered: # Write out any variance statistics for group in self.param_groups: for p in group['params']: param_state = self.state[p] #fg = param_state['full_grad'] #fg.zero_() # reset the full gradient for i in range(self.recalibration_interval): gi_var[i] += param_state["gi_var_acum"][i].sum()/self.recalibration_interval vr_var[i] += param_state["vr_var_acum"][i].sum()/self.recalibration_interval fname = 'stats/{}_scsg_{}.pkl'.format(self.test_name, self.epoch) with open(fname, 'wb') as output: pickle.dump({ 'gi_var': gi_var, 'vr_var': vr_var, 'epoch': self.epoch, }, output) #self.gradient_variances = [] #self.vr_step_variances = [] #self.batch_indices = [] self.stats_buffered = False print("logging pass diagnostics saved to {}".format(fname)) def recalibrate(self, batch_id, closure): """ SCSG doesn't use recalibration passes. """ return 0.0 def epoch_diagnostics(self, train_loss, train_err, test_loss, test_err): """ Called after recalibrate, saves stats out to disk. """ def step_outer_part(self, closure, idx): self.store_running_mean() loss = closure() for group in self.param_groups: for p in group['params']: gk = p.grad.data param_state = self.state[p] gavg = param_state['gavg'] gavg_debug = param_state['gavg_debug'] if self.recalibration_i == 0: param_state['tilde_x'].zero_().add_(p.data) gavg.zero_() gavg_debug.zero_() param_state['m2'].zero_() gavg_debug.add_(1.0/self.megabatch_size, gk) if not self.recompute_version: param_state['gktbl'][idx, :] = gk m2 = param_state['m2'] # Online mean/variance calcuation from wikipedia delta = gk - gavg gavg.add_(1.0/(self.recalibration_i+1), delta) #if self.recalibration_i == 4: # pdb.set_trace() delta2 = gk - gavg m2.add_(delta*delta2) self.restore_running_mean() self.recalibration_i += 1 #self.batches_processed += 1 return loss def step_inner_part(self, closure, idx): # Check a few things: if self.recalibration_i != self.megabatch_size: raise Exception("bad self.recalibration_i: {}".format(self.recalibration_i)) if self.recompute_version: self.store_running_mean() ## Store current xk, replace with x_tilde for group in self.param_groups: for p in group['params']: param_state = self.state[p] xk = param_state['xk'] xk.zero_().add_(p.data) p.data.zero_().add_(param_state['tilde_x']) ## Compute gradient at x_tilde closure() ## Store x_tilde gradient in gi, and revert to xk for group in self.param_groups: for p in group['params']: param_state = self.state[p] xk = param_state['xk'] param_state['gi'].zero_().add_(p.grad.data) p.data.zero_().add_(xk) self.restore_running_mean() # JUST FOR DEBUGGING if False: for group in self.param_groups: for p in group['params']: param_state = self.state[p] gi = param_state['gi'] gi_tbl = param_state['gktbl'][idx, :] #pdb.set_trace() if torch.norm(gi-gi_tbl) > 1e-6: print("difference: {}".format( torch.norm(gi-gi_tbl))) pdb.set_trace() else: for group in self.param_groups: for p in group['params']: param_state = self.state[p] param_state['gi'] = param_state['gktbl'][idx, :] ## compute gradient at xk loss = closure() for group in self.param_groups: momentum = group['momentum'] weight_decay = group['weight_decay'] learning_rate = group['lr'] for p in group['params']: gk = p.grad.data param_state = self.state[p] gi = param_state['gi'] gavg = param_state['gavg'] #gavg_debug = param_state['gavg_debug'] if momentum != 0: buf = param_state['momentum_buffer'] ######### if self.epoch < self.vr_from_epoch: vr_gradient = gk.clone() # Just do sgd steps else: vr_gradient = gk.clone().sub_(gi).add_(gavg) # Track the running mean and variance of the gradients. grad_running_mean = param_state['grad_running_mean'] grad_running_var = param_state['grad_running_var'] grad_running_cov = param_state['grad_running_cov'] cov_update = (gk - grad_running_mean)*(gi - gavg) grad_running_cov.mul_(self.running_interp).add_(1-self.running_interp, cov_update) # Using delta from before and after the mean update is apparently the # best way to update variances. delta1 = gk - grad_running_mean grad_running_mean.mul_(self.running_interp).add_(1-self.running_interp, gk) delta2 = gk - grad_running_mean var_update = delta1*delta2 grad_running_var.mul_(self.running_interp).add_(1-self.running_interp, var_update) #if torch.norm(gavg-gavg_debug) > 1e-7: # raise Exception("gavg norm diff: {}".format(torch.norm(gavg-gavg_debug))) if weight_decay != 0: vr_gradient.add_(weight_decay, p.data) if momentum != 0: dampening = 0.0 vr_gradient = buf.mul_(momentum).add_(1 - dampening, vr_gradient) # Take step. p.data.add_(-learning_rate, vr_gradient) # track number of minibatches seen #logging.info("interval i: {}".format(self.interval_i)) self.batches_processed += 1 if self.batches_processed % 20 == 0 and self.batches_processed > 0: running_cov_acum = 0.0 m2_acum = 0.0 var_acum = 0.0 for group in self.param_groups: for p in group['params']: param_state = self.state[p] grad_running_cov = param_state['grad_running_cov'] grad_running_var = param_state['grad_running_var'] m2 = param_state['m2'] running_cov_acum += grad_running_cov.sum() var_acum += grad_running_var.sum() # m2 is not stored normalized by self.nbatches m2_norm = m2.div(self.megabatch_size) m2_acum += m2_norm.sum() if m2_acum > 0: cov_var_ratio = running_cov_acum/m2_acum vr_variance = var_acum + m2_acum - 2*running_cov_acum vr_ratio = vr_variance/var_acum corr_coef = running_cov_acum/math.sqrt(var_acum*m2_acum) logging.info("VR RATIO: {:.3f}. Raw cov/var: {:.3f}, correlation coef: {:.3f}. Var: {:.3f} m2: {:.3f} cov: {:.3f}".format( vr_ratio, cov_var_ratio, corr_coef, var_acum, m2_acum, running_cov_acum)) return loss ############################################### ############################################### def full_grad_init(self): for group in self.param_groups: for p in group['params']: param_state = self.state[p] fg = param_state['full_grad'] fg.zero_() def full_grad_calc(self, closure): loss = closure() for group in self.param_groups: for p in group['params']: gk = p.grad.data param_state = self.state[p] fg = param_state['full_grad'] fg.add_(1.0/self.nbatches, gk) def logging_pass_start(self): self.recalibration_i = 0 self.interval_i = 0 self.stats_buffered = True for group in self.param_groups: for p in group['params']: param_state = self.state[p] pgz = p.grad.data.clone().zero_() #param_state["gi_mean_acum"] = [] param_state["gi_var_acum"] = [] #param_state["vr_mean_acum"] = [] param_state["vr_var_acum"] = [] for i in range(self.recalibration_interval): #param_state["gi_mean_acum"].append(pgz.clone()) param_state["gi_var_acum"].append(pgz.clone()) #param_state["vr_mean_acum"].append(pgz.clone()) param_state["vr_var_acum"].append(pgz.clone()) def logging_pass(self, interval, closure): self.store_running_mean() ## Store current xk, replace with x_tilde for group in self.param_groups: for p in group['params']: param_state = self.state[p] xk = param_state['xk'] xk.zero_().add_(p.data) p.data.zero_().add_(param_state['tilde_x']) ## Compute gradient at x_tilde closure() ## Store x_tilde gradient in gi, and revert to xk for group in self.param_groups: for p in group['params']: param_state = self.state[p] xk = param_state['xk'] param_state['gi'].zero_().add_(p.grad.data) p.data.zero_().add_(xk) # Restore running_mean/var self.restore_running_mean() ## compute gradient at xk loss = closure() for group in self.param_groups: momentum = group['momentum'] weight_decay = group['weight_decay'] learning_rate = group['lr'] for p in group['params']: gk = p.grad.data param_state = self.state[p] full_grad = param_state['full_grad'] gi = param_state['gi'] gavg = param_state['gavg'] vr_gradient = gk.clone().sub_(gi).add_(gavg) # Online mean/variance calcuation delta = gk - full_grad param_state["gi_var_acum"][interval].add_(delta*delta) # var version delta = vr_gradient - full_grad param_state["vr_var_acum"][interval].add_(delta*delta) return loss def store_running_mean(self): # Store running_mean/var temporarily state = self.model.state_dict() for skey in self.running_tmp.keys(): self.running_tmp[skey].zero_().add_(state[skey]) def restore_running_mean(self): state = self.model.state_dict() for skey in self.running_tmp.keys(): state[skey].zero_().add_(self.running_tmp[skey])
deep-variance-reduction-main
scsg.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from torch.optim.optimizer import Optimizer, required import torch import pdb import pickle import math import logging class RecomputeSVRG(Optimizer): r"""Implements the standard SVRG method """ def __init__(self, params, nbatches, model, vr_bn_at_recalibration, vr_from_epoch, lr=required, momentum=required, weight_decay=required): self.nbatches = nbatches self.batches_processed = 0 self.epoch = 0 self.vr_bn_at_recalibration = vr_bn_at_recalibration self.vr_from_epoch = vr_from_epoch self.model = model self.running_tmp = {} defaults = dict(lr=lr, momentum=momentum, weight_decay=weight_decay) super(RecomputeSVRG, self).__init__(params, defaults) def __setstate__(self, state): super(RecomputeSVRG, self).__setstate__(state) def initialize(self): for group in self.param_groups: for p in group['params']: momentum = group['momentum'] param_state = self.state[p] if 'gavg' not in param_state: param_state['gavg'] = p.data.double().clone().zero_() param_state['gi'] = p.data.clone().zero_() param_state['gi_debug'] = p.data.clone().zero_() if momentum != 0: if 'momentum_buffer' not in param_state: buf = param_state['momentum_buffer'] = p.data.clone().zero_() if 'tilde_x' not in param_state: param_state['tilde_x'] = p.data.clone() param_state['xk'] = p.data.clone() # Batch norm's activation running_mean/var variables state = self.model.state_dict() for skey in state.keys(): if skey.endswith(".running_mean") or skey.endswith(".running_var"): self.running_tmp[skey] = state[skey].clone() def recalibrate_start(self): """ Part of the recalibration pass with SVRG. Stores the gradients for later use. """ self.epoch += 1 self.recal_calls = 0 self.initialize() self.store_running_mean() print("Recal epoch: {}".format(self.epoch)) if self.epoch >= self.vr_from_epoch: for group in self.param_groups: for p in group['params']: param_state = self.state[p] gavg = param_state['gavg'] gavg.zero_() tilde_x = param_state['tilde_x'] tilde_x.zero_().add_(p.data.clone()) #pdb.set_trace() else: logging.info("Skipping recalibration as epoch {} not >= {}".format( self.epoch, self.vr_from_epoch)) def recalibrate(self, batch_id, closure): """ Compute part of the full batch gradient, from one minibatch """ loss = closure() if self.epoch >= self.vr_from_epoch: self.recal_calls += 1 for group in self.param_groups: for p in group['params']: gk = p.grad.data param_state = self.state[p] gavg = param_state['gavg'] gavg.add_(1.0/self.nbatches, gk.double()) return loss def recalibrate_end(self): self.restore_running_mean() if self.recal_calls != self.nbatches: raise Exception("recalibrate_end called, with {} nbatches: {}".format( self.recal_calls, self.nbatches)) def epoch_diagnostics(self, train_loss, train_err, test_loss, test_err): """ Called after recalibrate, saves stats out to disk. """ def step(self, batch_id, closure): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ if self.epoch >= self.vr_from_epoch: self.store_running_mean() ## Store current xk, replace with x_tilde for group in self.param_groups: for p in group['params']: param_state = self.state[p] xk = param_state['xk'] xk.zero_().add_(p.data) p.data.zero_().add_(param_state['tilde_x']) # Standard is vr_bn_at_recalibration=True, so this doesn't fire. if not self.vr_bn_at_recalibration: self.model.eval() # turn off batch norm ## Compute gradient at x_tilde closure() ## Store x_tilde gradient in gi, and revert to xk for group in self.param_groups: for p in group['params']: param_state = self.state[p] xk = param_state['xk'] gi = param_state['gi'] gi.zero_().add_(p.grad.data) p.data.zero_().add_(xk) # Make sure batchnorm is handled correctly. self.restore_running_mean() ## compute gradient at xk loss = closure() for group in self.param_groups: momentum = group['momentum'] weight_decay = group['weight_decay'] learning_rate = group['lr'] for p in group['params']: gk = p.grad.data param_state = self.state[p] gi = param_state['gi'] gavg = param_state['gavg'] if momentum != 0: buf = param_state['momentum_buffer'] ######### if self.epoch >= self.vr_from_epoch: vr_gradient = gk.clone().sub_(gi).add_(gavg.type_as(gk)) else: vr_gradient = gk.clone() # Just do sgd steps if weight_decay != 0: vr_gradient.add_(weight_decay, p.data) if momentum != 0: dampening = 0.0 vr_gradient = buf.mul_(momentum).add_(1 - dampening, vr_gradient) # Take step. p.data.add_(-learning_rate, vr_gradient) # track number of minibatches seen self.batches_processed += 1 return loss def store_running_mean(self): # Store running_mean/var temporarily state = self.model.state_dict() #pdb.set_trace() for skey in self.running_tmp.keys(): self.running_tmp[skey].zero_().add_(state[skey]) def restore_running_mean(self): state = self.model.state_dict() for skey in self.running_tmp.keys(): state[skey].zero_().add_(self.running_tmp[skey])
deep-variance-reduction-main
recompute_svrg.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import torch import torch.multiprocessing as multiprocessing #from torch._C import _update_worker_pids, \ # _remove_worker_pids, _error_if_any_worker_fails #from .sampler import SequentialSampler, RandomSampler, BatchSampler from torch.utils.data.sampler import SequentialSampler, RandomSampler, BatchSampler import signal import functools import collections import re import sys import traceback import threading from torch._six import string_classes, int_classes if sys.version_info[0] == 2: import Queue as queue else: import queue _use_shared_memory = False """Whether to use shared memory in default_collate""" class ExceptionWrapper(object): "Wraps an exception plus traceback to communicate across threads" def __init__(self, exc_info): self.exc_type = exc_info[0] self.exc_msg = "".join(traceback.format_exception(*exc_info)) def _worker_loop(dataset, index_queue, data_queue, collate_fn, seed, init_fn, worker_id): global _use_shared_memory _use_shared_memory = True # Intialize C side signal handlers for SIGBUS and SIGSEGV. Python signal # module's handlers are executed after Python returns from C low-level # handlers, likely when the same fatal signal happened again already. # https://docs.python.org/3/library/signal.html Sec. 18.8.1.1 #_set_worker_signal_handlers() torch.set_num_threads(1) torch.manual_seed(seed) if init_fn is not None: init_fn(worker_id) while True: r = index_queue.get() if r is None: break idx, batch_indices = r try: samples = collate_fn([dataset[i] for i in batch_indices]) except Exception: data_queue.put((idx, ExceptionWrapper(sys.exc_info()))) else: data_queue.put((idx, samples)) def _worker_manager_loop(in_queue, out_queue, done_event, pin_memory, device_id): if pin_memory: torch.cuda.set_device(device_id) while True: try: r = in_queue.get() except Exception: if done_event.is_set(): return raise if r is None: break if isinstance(r[1], ExceptionWrapper): out_queue.put(r) continue idx, batch = r try: if pin_memory: batch = pin_memory_batch(batch) except Exception: out_queue.put((idx, ExceptionWrapper(sys.exc_info()))) else: out_queue.put((idx, batch)) numpy_type_map = { 'float64': torch.DoubleTensor, 'float32': torch.FloatTensor, 'float16': torch.HalfTensor, 'int64': torch.LongTensor, 'int32': torch.IntTensor, 'int16': torch.ShortTensor, 'int8': torch.CharTensor, 'uint8': torch.ByteTensor, } def default_collate(batch): "Puts each data field into a tensor with outer dimension batch size" error_msg = "batch must contain tensors, numbers, dicts or lists; found {}" elem_type = type(batch[0]) if torch.is_tensor(batch[0]): out = None if _use_shared_memory: # If we're in a background process, concatenate directly into a # shared memory tensor to avoid an extra copy numel = sum([x.numel() for x in batch]) storage = batch[0].storage()._new_shared(numel) out = batch[0].new(storage) return torch.stack(batch, 0, out=out) elif elem_type.__module__ == 'numpy' and elem_type.__name__ != 'str_' \ and elem_type.__name__ != 'string_': elem = batch[0] if elem_type.__name__ == 'ndarray': # array of string classes and object if re.search('[SaUO]', elem.dtype.str) is not None: raise TypeError(error_msg.format(elem.dtype)) return torch.stack([torch.from_numpy(b) for b in batch], 0) if elem.shape == (): # scalars py_type = float if elem.dtype.name.startswith('float') else int return numpy_type_map[elem.dtype.name](list(map(py_type, batch))) elif isinstance(batch[0], int_classes): return torch.LongTensor(batch) elif isinstance(batch[0], float): return torch.DoubleTensor(batch) elif isinstance(batch[0], string_classes): return batch elif isinstance(batch[0], collections.Mapping): return {key: default_collate([d[key] for d in batch]) for key in batch[0]} elif isinstance(batch[0], collections.Sequence): transposed = zip(*batch) return [default_collate(samples) for samples in transposed] raise TypeError((error_msg.format(type(batch[0])))) def pin_memory_batch(batch): if torch.is_tensor(batch): return batch.pin_memory() elif isinstance(batch, string_classes): return batch elif isinstance(batch, collections.Mapping): return {k: pin_memory_batch(sample) for k, sample in batch.items()} elif isinstance(batch, collections.Sequence): return [pin_memory_batch(sample) for sample in batch] else: return batch _SIGCHLD_handler_set = False """Whether SIGCHLD handler is set for DataLoader worker failures. Only one handler needs to be set for all DataLoaders in a process.""" def _set_SIGCHLD_handler(): if sys.platform == 'win32': # Windows doesn't support SIGCHLD handler return global _SIGCHLD_handler_set if _SIGCHLD_handler_set: return previous_handler = signal.getsignal(signal.SIGCHLD) if not callable(previous_handler): previous_handler = None def handler(signum, frame): # This following call uses `waitid` with WNOHANG from C side. Therefore, # Python can still get and update the process status successfully. #_error_if_any_worker_fails() if previous_handler is not None: previous_handler(signum, frame) signal.signal(signal.SIGCHLD, handler) _SIGCHLD_handler_set = True class DataLoaderIter(object): "Iterates once over the DataLoader's dataset, as specified by the sampler" def __init__(self, loader): self.dataset = loader.dataset self.collate_fn = loader.collate_fn self.batch_sampler = loader.batch_sampler self.num_workers = loader.num_workers self.pin_memory = loader.pin_memory and torch.cuda.is_available() self.timeout = loader.timeout self.done_event = threading.Event() self.sample_iter = iter(self.batch_sampler) if self.num_workers > 0: self.worker_init_fn = loader.worker_init_fn self.index_queue = multiprocessing.SimpleQueue() self.worker_result_queue = multiprocessing.SimpleQueue() self.batches_outstanding = 0 self.worker_pids_set = False self.shutdown = False self.send_idx = 0 self.rcvd_idx = 0 self.reorder_dict = {} base_seed = torch.LongTensor(1).random_()[0] self.workers = [ multiprocessing.Process( target=_worker_loop, args=(self.dataset, self.index_queue, self.worker_result_queue, self.collate_fn, base_seed + i, self.worker_init_fn, i)) for i in range(self.num_workers)] if self.pin_memory or self.timeout > 0: self.data_queue = queue.Queue() self.worker_manager_thread = threading.Thread( target=_worker_manager_loop, args=(self.worker_result_queue, self.data_queue, self.done_event, self.pin_memory, torch.cuda.current_device())) self.worker_manager_thread.daemon = True self.worker_manager_thread.start() else: self.data_queue = self.worker_result_queue for w in self.workers: w.daemon = True # ensure that the worker exits on process exit w.start() #_update_worker_pids(id(self), tuple(w.pid for w in self.workers)) _set_SIGCHLD_handler() self.worker_pids_set = True # prime the prefetch loop for _ in range(2 * self.num_workers): self._put_indices() def __len__(self): return len(self.batch_sampler) def _get_batch(self): if self.timeout > 0: try: return self.data_queue.get(True, self.timeout) except queue.Empty: raise RuntimeError('DataLoader timed out after {} seconds'.format(self.timeout)) else: return self.data_queue.get() def __next__(self): if self.num_workers == 0: # same-process loading indices = next(self.sample_iter) # may raise StopIteration batch = self.collate_fn([self.dataset[i] for i in indices]) if self.pin_memory: batch = pin_memory_batch(batch) return batch # check if the next sample has already been generated if self.rcvd_idx in self.reorder_dict: batch = self.reorder_dict.pop(self.rcvd_idx) return self._process_next_batch(batch) if self.batches_outstanding == 0: self._shutdown_workers() raise StopIteration while True: assert (not self.shutdown and self.batches_outstanding > 0) idx, batch = self._get_batch() self.batches_outstanding -= 1 if idx != self.rcvd_idx: # store out-of-order samples self.reorder_dict[idx] = batch continue return self._process_next_batch(batch) next = __next__ # Python 2 compatibility def __iter__(self): return self def _put_indices(self): assert self.batches_outstanding < 2 * self.num_workers indices = next(self.sample_iter, None) if indices is None: return self.index_queue.put((self.send_idx, indices)) self.batches_outstanding += 1 self.send_idx += 1 def _process_next_batch(self, batch): self.rcvd_idx += 1 self._put_indices() if isinstance(batch, ExceptionWrapper): raise batch.exc_type(batch.exc_msg) return batch def __getstate__(self): # TODO: add limited pickling support for sharing an iterator # across multiple threads for HOGWILD. # Probably the best way to do this is by moving the sample pushing # to a separate thread and then just sharing the data queue # but signalling the end is tricky without a non-blocking API raise NotImplementedError("DataLoaderIterator cannot be pickled") def _shutdown_workers(self): if not self.shutdown: self.shutdown = True self.done_event.set() # if worker_manager_thread is waiting to put while not self.data_queue.empty(): self.data_queue.get() for _ in self.workers: self.index_queue.put(None) # done_event should be sufficient to exit worker_manager_thread, but # be safe here and put another None self.worker_result_queue.put(None) if self.worker_pids_set: #_remove_worker_pids(id(self)) self.worker_pids_set = False def __del__(self): if self.num_workers > 0: self._shutdown_workers() class DataLoader(object): """ Data loader. Combines a dataset and a sampler, and provides single- or multi-process iterators over the dataset. Arguments: dataset (Dataset): dataset from which to load the data. batch_size (int, optional): how many samples per batch to load (default: 1). shuffle (bool, optional): set to ``True`` to have the data reshuffled at every epoch (default: False). sampler (Sampler, optional): defines the strategy to draw samples from the dataset. If specified, ``shuffle`` must be False. batch_sampler (Sampler, optional): like sampler, but returns a batch of indices at a time. Mutually exclusive with batch_size, shuffle, sampler, and drop_last. num_workers (int, optional): how many subprocesses to use for data loading. 0 means that the data will be loaded in the main process. (default: 0) collate_fn (callable, optional): merges a list of samples to form a mini-batch. pin_memory (bool, optional): If ``True``, the data loader will copy tensors into CUDA pinned memory before returning them. drop_last (bool, optional): set to ``True`` to drop the last incomplete batch, if the dataset size is not divisible by the batch size. If ``False`` and the size of dataset is not divisible by the batch size, then the last batch will be smaller. (default: False) timeout (numeric, optional): if positive, the timeout value for collecting a batch from workers. Should always be non-negative. (default: 0) worker_init_fn (callable, optional): If not None, this will be called on each worker subprocess with the worker id as input, after seeding and before data loading. (default: None) .. note:: By default, each worker will have its PyTorch seed set to ``base_seed + worker_id``, where ``base_seed`` is a long generated by main process using its RNG. You may use ``torch.initial_seed()`` to access this value in :attr:`worker_init_fn`, which can be used to set other seeds (e.g. NumPy) before data loading. .. warning:: If ``spawn'' start method is used, :attr:`worker_init_fn` cannot be an unpicklable object, e.g., a lambda function. """ def __init__(self, dataset, batch_size=1, shuffle=False, sampler=None, batch_sampler=None, num_workers=0, collate_fn=default_collate, pin_memory=False, drop_last=False, timeout=0, worker_init_fn=None): self.dataset = dataset self.batch_size = batch_size self.num_workers = num_workers self.collate_fn = collate_fn self.pin_memory = pin_memory self.drop_last = drop_last self.timeout = timeout self.worker_init_fn = worker_init_fn if timeout < 0: raise ValueError('timeout option should be non-negative') if batch_sampler is not None: if batch_size > 1 or shuffle or sampler is not None or drop_last: raise ValueError('batch_sampler is mutually exclusive with ' 'batch_size, shuffle, sampler, and drop_last') if sampler is not None and shuffle: raise ValueError('sampler is mutually exclusive with shuffle') if self.num_workers < 0: raise ValueError('num_workers cannot be negative; ' 'use num_workers=0 to disable multiprocessing.') if batch_sampler is None: if sampler is None: if shuffle: sampler = RandomSampler(dataset) else: sampler = SequentialSampler(dataset) batch_sampler = BatchSampler(sampler, batch_size, drop_last) self.sampler = sampler self.batch_sampler = batch_sampler def __iter__(self): return DataLoaderIter(self) def __len__(self): return len(self.batch_sampler)
deep-variance-reduction-main
UpdatedDataLoaderMult.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from __future__ import print_function import argparse import pickle import os from timeit import default_timer as timer import torch import torch.nn as nn from torch.autograd import Variable import sys import math import problems import optimizers import logging import pdb from torch.nn.functional import nll_loss, log_softmax import numpy as np def str2bool(v): if v.lower() in ('yes', 'true', 't', 'y', '1'): return True elif v.lower() in ('no', 'false', 'f', 'n', '0'): return False else: raise argparse.ArgumentTypeError('Boolean value expected.') ####################################################################### class Struct: def __init__(self, **entries): self.__dict__.update(entries) def in_run_diagnostics(epoch, batch_idx, args, train_loader, optimizer, model, criterion): #logging.info("in run diagnostics invoked") if (epoch % 10) == 0 or args.log_diagnostics_every_epoch: nbatches = len(train_loader) if args.log_diagnostics_deciles: log_intervals = math.ceil(nbatches/10.0) log_now = batch_idx % log_intervals == 0 else: lp = math.ceil(nbatches/100.0) log_now = batch_idx == int(math.ceil(nbatches/50.0)) log_now = log_now or batch_idx == int(math.ceil(nbatches/9.0)) log_now = log_now or batch_idx == int(math.ceil(nbatches/3.0)) log_now = log_now or batch_idx == nbatches-1 if log_now: print("interval, batch_idx = {}".format(batch_idx)) optimizer.logging_pass_start() if optimizer.epoch >= optimizer.vr_from_epoch: for inner_batch_idx, (data, target) in enumerate(train_loader): if args.cuda: data, target = data.cuda(), target.cuda() data, target = Variable(data), Variable(target) def eval_closure(): optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() return loss optimizer.logging_pass(inner_batch_idx, closure=eval_closure) logging.info("Logging pass finished") optimizer.logging_pass_end(batch_idx) def online_svrg_diagnostics(epoch, batch_idx, args, train_loader, optimizer, model, criterion): if (epoch == 1 or (epoch % 10) == 0) and optimizer.epoch >= optimizer.vr_from_epoch and batch_idx == 0: nbatches = len(train_loader) mega_batch_size = optimizer.megabatch_size recalibration_interval = optimizer.recalibration_interval #print("interval, interval = {}".format(interval)) optimizer.logging_pass_start() # Compute the snapshot snapshot_i = 0 for inner_batch_idx, (data, target) in enumerate(train_loader): if args.cuda: data, target = data.cuda(), target.cuda() data, target = Variable(data), Variable(target) def eval_closure(): optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() return loss optimizer.snapshot_pass(inner_batch_idx, closure=eval_closure) snapshot_i += 1 if snapshot_i == mega_batch_size: break logging.info("Snapshot computed") for interval in range(recalibration_interval): logging.info("Interval: {}, recal_i: {}".format(interval, optimizer.recalibration_i)) optimizer.full_grad_init() # Do a full gradient calculation: for inner_batch_idx, (data, target) in enumerate(train_loader): if args.cuda: data, target = data.cuda(), target.cuda() data, target = Variable(data), Variable(target) def eval_closure(): optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() return loss optimizer.full_grad_calc(inner_batch_idx, closure=eval_closure) logging.info("Full grad calculation finished") for inner_batch_idx, (data, target) in enumerate(train_loader): if args.cuda: data, target = data.cuda(), target.cuda() data, target = Variable(data), Variable(target) def eval_closure(): optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() return loss optimizer.logging_pass(interval, inner_batch_idx, closure=eval_closure) logging.info("Logging pass finished") # Take a single step at the end to progress in the interval # Using whatever minibatch was last in the stats logging pass optimizer.step(inner_batch_idx, closure=eval_closure) def scsg_diagnostics(epoch, args, train_loader, optimizer, model, criterion): if (epoch == 1 or (epoch % 10) == 0) and optimizer.epoch >= optimizer.vr_from_epoch: nbatches = len(train_loader) mega_batch_size = optimizer.megabatch_size recalibration_interval = optimizer.recalibration_interval #print("interval, interval = {}".format(interval)) optimizer.logging_pass_start() # Compute the snapshot data_buffer = [] inner_iters = optimizer.recalibration_interval megabatch_size = optimizer.megabatch_size optimizer.recalibration_i = 0 for batch_idx, (data, target) in enumerate(train_loader): batch_id = batch_idx if args.cuda: data, target = data.cuda(), target.cuda() data, target = Variable(data), Variable(target) data_buffer.append((data, target)) # Store megabatch gradients def outer_closure(): optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() return loss loss = optimizer.step_outer_part(closure=outer_closure) if len(data_buffer) == megabatch_size: logging.info("Snapshot complete") for interval in range(recalibration_interval): logging.info("Interval: {}, recal_i: {}".format(interval, optimizer.recalibration_i)) optimizer.full_grad_init() # Do a full gradient calculation: for inner_batch_idx, (data, target) in enumerate(train_loader): if args.cuda: data, target = data.cuda(), target.cuda() data, target = Variable(data), Variable(target) def eval_closure(): optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() return loss optimizer.full_grad_calc(closure=eval_closure) logging.info("Full grad calculation finished") for inner_i in range(inner_iters): data, target = data_buffer[inner_i] def eval_closure(): optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() return loss optimizer.logging_pass(interval, closure=eval_closure) logging.info("Logging pass finished") # Take a single step at the end to progress in the interval data, target = data_buffer[interval] def eval_closure(): optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() return loss optimizer.step_inner_part(closure=eval_closure) data_buffer = [] optimizer.recalibration_i = 0 return def minibatch_stats(): # This is just copied from run.py, needs to be modified to work. if False: batch_idx, (data, target) = next(enumerate(train_loader)) if args.cuda: data, target = data.cuda(), target.cuda() data, target = Variable(data), Variable(target) idx = 0 ### optimizer.zero_grad() output = model(data) #pdb.set_trace() loss = criterion(output[idx, None], target[idx]) loss.backward() baseline_sq = 0.0 for group in optimizer.param_groups: for p in group['params']: gk = p.grad.data param_state = optimizer.state[p] param_state['baseline'] = gk.clone() baseline_sq += torch.dot(gk, gk) for idx in range(1, 5): optimizer.zero_grad() output = model(data) loss = criterion(output[idx, None], target[idx]) loss.backward() total_dot = 0.0 square_norm = 0.0 corrs = [] for group in optimizer.param_groups: for p in group['params']: gk = p.grad.data param_state = optimizer.state[p] baseline = param_state['baseline'] # Compute correlation dp = torch.dot(baseline, gk) corr = dp/(torch.norm(baseline)*torch.norm(gk)) corrs.append(corr) total_dot += dp square_norm += torch.dot(gk, gk) total_corr = total_dot/math.sqrt(square_norm*baseline_sq) logging.info("i={}, corr: {}, layers: {}".format(idx, total_corr, corrs)) #pdb.set_trace() def batchnorm_diagnostics(epoch, args, train_loader, optimizer, model): #pdb.set_trace() bnstuff = {'epoch': epoch, 'args': args} state = model.state_dict() for skey in state.keys(): if skey.startswith("bn3") or skey.startswith("fc1"): # Convert to numpy first bnstuff[skey] = state[skey].cpu().numpy() #print("skey: {} size: {}".format(skey, state[skey].size())) # Search optimizer state for param_state for this variable for group in optimizer.param_groups: for p in group['params']: gk = p.grad.data param_state = optimizer.state[p] if id(p.data) == id(state[skey]): #print("match") bnstuff[skey + ".gavg"] = param_state["gavg"].cpu().numpy() # Store to disk I guess fname = 'stats/{}_batchnorm_epoch{}.pkl'.format(args.logfname, epoch) with open(fname, 'wb') as output: pickle.dump(bnstuff, output) logging.info("Wrote out batch norm stats: {}".format(fname))
deep-variance-reduction-main
diagnostics.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from torchvision import datasets, transforms from torch.utils.data.sampler import RandomSampler import torchvision import torchvision.transforms as transforms import caching_transforms import torch.utils.data import pdb import logging import numpy as np from multiprocessing.sharedctypes import RawArray from ctypes import Structure, c_double class ImagenetWrapper(torch.utils.data.Dataset): def __init__(self, root, lock_transforms): global transform_instances self.dataset = datasets.ImageFolder(root) self.nimages = len(self.dataset) self.rand_per_image = 6 self.transform_instances = RawArray(c_double, self.rand_per_image*self.nimages) transform_instances = self.transform_instances self.normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # These modified transforms take the random numbers they need explicitly # as arguments to the transform method. self.crop = caching_transforms.RandomResizedCrop(224) self.flip = caching_transforms.RandomHorizontalFlip() # Initial transformation cache created. self.retransform() # Must be called on each child process def child_initialize(self, worker_id): global transform_instances transform_instances = self.transform_instances # self is parent processes version global_worker_id = worker_id print("child process: {}".format(global_worker_id)) def __getitem__(self, index): global transform_instances item = self.dataset[index] img, lbl = item if index == 0: self.print_rands() # Apply transforms using saved random numbers start = index*self.rand_per_image transformed_img = self.crop.transform(img, transform_instances[start], transform_instances[start+1], transform_instances[start+2], transform_instances[start+3], transform_instances[start+4]) transformed_img = self.flip.transform(transformed_img, transform_instances[start+5]) transformed_img = self.normalize(caching_transforms.to_tensor(transformed_img)) return (transformed_img, lbl) def __len__(self): #return 1024 return len(self.dataset) def retransform(self): np_instances = np.frombuffer(self.transform_instances) # Generate all the random numbers logging.info("Generating {} random numbers ...".format(len(self.transform_instances))) np_instances[:] = np.random.uniform(size=len(self.transform_instances)) logging.info("Numbers generated") self.print_rands() def print_rands(self): global transform_instances start = 0 #pdb.set_trace() print("len: {}. r1 {} r2 {} r3 {} r4 {} r5 {} r6 {}".format( len(transform_instances), transform_instances[start], transform_instances[start+1], transform_instances[start+2], transform_instances[start+3], transform_instances[start+4], transform_instances[start+5] ))
deep-variance-reduction-main
imagenet_wrapper.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import torch import torch.nn as nn import torch.nn.functional as F import math import pdb __all__ = ['densenet'] from torch.autograd import Variable class Bottleneck(nn.Module): def __init__(self, inplanes, batchnorm, nonlinearity, expansion=4, growthRate=12, dropRate=0): super(Bottleneck, self).__init__() planes = expansion * growthRate self.batchnorm = batchnorm if self.batchnorm: self.bn1 = nn.BatchNorm2d(inplanes) self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) if self.batchnorm: self.bn2 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d(planes, growthRate, kernel_size=3, padding=1, bias=False) self.nonlinearity = nonlinearity self.dropRate = dropRate def forward(self, x): out = x if self.batchnorm: out = self.bn1(out) out = self.nonlinearity(out) out = self.conv1(out) if self.batchnorm: out = self.bn2(out) out = self.nonlinearity(out) out = self.conv2(out) if self.dropRate > 0: out = F.dropout(out, p=self.dropRate, training=self.training) out = torch.cat((x, out), 1) return out class BasicBlock(nn.Module): def __init__(self, inplanes, batchnorm, nonlinearity, expansion=1, growthRate=12, dropRate=0): super(BasicBlock, self).__init__() planes = expansion * growthRate self.batchnorm = batchnorm if self.batchnorm: self.bn1 = nn.BatchNorm2d(inplanes) self.conv1 = nn.Conv2d(inplanes, growthRate, kernel_size=3, padding=1, bias=False) self.nonlinearity = nonlinearity self.dropRate = dropRate def forward(self, x): out = x if self.batchnorm: out = self.bn1(out) out = self.nonlinearity(out) out = self.conv1(out) if self.dropRate > 0: out = F.dropout(out, p=self.dropRate, training=self.training) out = torch.cat((x, out), 1) return out class Transition(nn.Module): def __init__(self, inplanes, outplanes, batchnorm, nonlinearity): super(Transition, self).__init__() self.batchnorm = batchnorm if self.batchnorm: self.bn1 = nn.BatchNorm2d(inplanes) self.conv1 = nn.Conv2d(inplanes, outplanes, kernel_size=1, bias=False) self.nonlinearity = nonlinearity def forward(self, x): out = x if self.batchnorm: out = self.bn1(out) out = self.nonlinearity(out) out = self.conv1(out) out = F.avg_pool2d(out, 2) return out class DenseNet(nn.Module): def __init__(self, depth=22, block=Bottleneck, dropRate=0, num_classes=10, growthRate=12, compressionRate=2, batchnorm=True, nonlinearity=None): super(DenseNet, self).__init__() assert (depth - 4) % 3 == 0, 'depth should be 3n+4' n = (depth - 4) / 3 if block == BasicBlock else (depth - 4) // 6 self.growthRate = growthRate self.dropRate = dropRate self.batchnorm = batchnorm self.nonlinearity = nonlinearity # self.inplanes is a global variable used across multiple # helper functions self.inplanes = growthRate * 2 self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=3, padding=1, bias=False) self.dense1 = self._make_denseblock(block, n) self.trans1 = self._make_transition(compressionRate) self.dense2 = self._make_denseblock(block, n) self.trans2 = self._make_transition(compressionRate) self.dense3 = self._make_denseblock(block, n) if self.batchnorm: self.bn = nn.BatchNorm2d(self.inplanes) self.avgpool = nn.AvgPool2d(8) self.fc = nn.Linear(self.inplanes, num_classes) # Weight initialization for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() def _make_denseblock(self, block, blocks): layers = [] for i in range(blocks): # Currently we fix the expansion ratio as the default value layers.append(block( self.inplanes, self.batchnorm, self.nonlinearity, growthRate=self.growthRate, dropRate=self.dropRate)) self.inplanes += self.growthRate return nn.Sequential(*layers) def _make_transition(self, compressionRate): inplanes = self.inplanes outplanes = int(math.floor(self.inplanes // compressionRate)) self.inplanes = outplanes return Transition(inplanes, outplanes, self.batchnorm, self.nonlinearity) def forward(self, x): x = self.conv1(x) x = self.trans1(self.dense1(x)) x = self.trans2(self.dense2(x)) x = self.dense3(x) if self.batchnorm: x = self.bn(x) x = self.nonlinearity(x) x = self.avgpool(x) x = x.view(x.size(0), -1) x = self.fc(x) return x def densenet(**kwargs): """ Constructs a DenseNet model. """ return DenseNet(**kwargs)
deep-variance-reduction-main
densenet.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from __future__ import print_function import argparse import pickle import os from timeit import default_timer as timer import torch import torch.nn as nn from torch.autograd import Variable import sys import math import datetime import problems import optimizers import logging import pdb from torch.nn.functional import nll_loss, log_softmax import numpy as np def recalibrate(epoch, args, train_loader, test_loader, model, train_dataset, optimizer, criterion): if args.vr_bn_at_recalibration: model.train() else: model.eval() logging.info("Recalibration pass starting") if hasattr(optimizer, "recalibrate_start"): optimizer.recalibrate_start() start = timer() #logging.info("Recalibration loop ...") if optimizer.epoch >= optimizer.vr_from_epoch and args.method != "online_svrg" and args.method != "scsg": for batch_idx, (data, target) in enumerate(train_loader): batch_id = batch_idx #pdb.set_trace() if args.cuda: data, target = data.cuda(), target.cuda(non_blocking=True) data, target = Variable(data), Variable(target) #print("recal:") #print(data[:2].data.cpu().numpy()) def eval_closure(): optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() return loss optimizer.recalibrate(batch_id, closure=eval_closure) if batch_idx % args.log_interval == 0: mid = timer() percent_done = 100. * batch_idx / len(train_loader) if percent_done > 0: time_estimate = math.ceil((mid - start)*(100/percent_done)) time_estimate = str(datetime.timedelta(seconds=time_estimate)) else: time_estimate = "unknown" logging.info('Recal Epoch: {} [{}/{} ({:.0f}%)] estimate: {}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), percent_done, time_estimate)) if hasattr(optimizer, "recalibrate_end"): optimizer.recalibrate_end() logging.info("Recalibration finished") def train_scsg(epoch, args, train_loader, test_loader, model, train_dataset, optimizer, criterion): logging.info("Train (SCSG version)") model.train() data_buffer = [] inner_iters = optimizer.recalibration_interval megabatch_size = optimizer.megabatch_size optimizer.recalibration_i = 0 logged = False for batch_idx, (data, target) in enumerate(train_loader): if args.cuda: data, target = data.cuda(), target.cuda() data, target = Variable(data), Variable(target) # Store megabatch gradients def outer_closure(): optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() return loss loss = optimizer.step_outer_part(closure=outer_closure, idx=len(data_buffer)) data_buffer.append((data, target)) # When data-buffer is full, do the actual inner steps. if len(data_buffer) == megabatch_size: for inner_i in range(inner_iters): data, target = data_buffer[inner_i] def eval_closure(): optimizer.zero_grad() output = model(data) loss = criterion(output, target) loss.backward() return loss optimizer.step_inner_part(closure=eval_closure, idx=inner_i) data_buffer = [] optimizer.recalibration_i = 0 if not logged and args.log_diagnostics and epoch >= args.vr_from_epoch: scsg_diagnostics(epoch, args, train_loader, optimizer, model, criterion) logged = True if batch_idx % args.log_interval == 0: logging.info('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.data.item())) if hasattr(model, "sampler") and hasattr(model.sampler, "reorder"): model.sampler.reorder() if hasattr(train_dataset, "retransform"): logging.info("retransform") train_dataset.retransform()
deep-variance-reduction-main
run_vr.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import torch import pdb import os class VRSamplerIter(object): def __init__(self, sampler): self.sampler = sampler self.i = 0 def __next__(self): #if self.sampler.creation_process != os.getpid(): # print("__next__ called on child process") self.i += 1 if self.i > self.sampler.nbatches: raise StopIteration else: return self.sampler.batches[self.i-1] def __len__(self): return self.sampler.nbatches class VRSampler(object): """Wraps two samplers to craete a sampler object suitable for use with variance reduction. methods Args: initial_sampler (Sampler): Base sampler for initial ordering. order (string) Either inorder, perm or random. batch_size (int): Size of mini-batch. drop_last (bool): If ``True``, the sampler will drop the last batch if its size would be less than ``batch_size`` Example: list(VRSampler(range(10), order="inorder", batch_size=3, drop_last=False)) """ def __init__(self, order, batch_size, dataset_size, drop_last=False): self.order = order self.batch_size = batch_size self.dataset_size = dataset_size self.drop_last = drop_last self.creation_process = os.getpid() self.reorder() def reorder(self): if self.creation_process != os.getpid(): raise Exception("reorder called on child process, which is bad. {} got: {}".format(self.creation_process, os.getpid())) print("Reordering instances: {}".format(self.order)) if self.order == "perm": idx_list = torch.randperm(self.dataset_size) else: idx_list = (torch.rand(self.dataset_size)*self.dataset_size).long() # Generate initial minibatches self.batches = [] batch = [] for idx in idx_list: batch.append(idx) if len(batch) == self.batch_size: self.batches.append(batch) batch = [] if len(batch) > 0 and not self.drop_last: self.batches.append(batch) self.nbatches = len(self.batches) #pdb.set_trace() def __iter__(self): print("Sampler __iter__") return VRSamplerIter(self) def __len__(self): return self.nbatches
deep-variance-reduction-main
vr_sampler.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from torch.optim.optimizer import Optimizer, required import torch import pdb import pickle import math import logging import scipy import scipy.stats import scipy.stats.mstats class SVRG(Optimizer): r"""Implements the standard SVRG method """ def __init__(self, params, args, nbatches, lr=required, momentum=required, weight_decay=required): self.nbatches = nbatches self.batches_processed = 0 self.epoch = 0 self.vr_from_epoch = args.vr_from_epoch self.test_name = args.logfname #"densenet" #"resnet-" #"sgd-" #"resnet-" if args.transform_locking: self.test_name += "_LOCK_" else: self.test_name += "_ULOCK_" self.recalibration_i = 0 self.running_interp = 0.9 self.denom_epsilon = 1e-7 # avoid divide by zero defaults = dict(lr=lr, momentum=momentum, weight_decay=weight_decay) self.gradient_variances = [] self.vr_step_variances = [] self.batch_indices = [] self.iterate_distances = [] self.inrun_iterate_distances = [] self.inrun_grad_distances = [] super(SVRG, self).__init__(params, defaults) def __setstate__(self, state): super(SVRG, self).__setstate__(state) def initialize(self): m = self.nbatches for group in self.param_groups: for p in group['params']: momentum = group['momentum'] gsize = p.data.size() gtbl_size = torch.Size([m] + list(gsize)) param_state = self.state[p] if 'gktbl' not in param_state: param_state['gktbl'] = torch.zeros(gtbl_size) param_state['logging_gktbl'] = torch.zeros(gtbl_size) if 'tilde_x' not in param_state: param_state['tilde_x'] = p.data.clone() param_state['running_x'] = p.data.clone() if 'gavg' not in param_state: param_state['gavg'] = p.data.clone().double().zero_() param_state['logging_gavg'] = p.data.clone().double().zero_() param_state['m2'] = p.data.clone().double().zero_() param_state['running_cov'] = p.data.clone().double().zero_() param_state['running_mean'] = p.data.clone().double().zero_() if momentum != 0: if 'momentum_buffer' not in param_state: buf = param_state['momentum_buffer'] = p.data.clone().zero_() def store_old_table(self): """ Stores the old gradient table for recalibration purposes. """ for group in self.param_groups: for p in group['params']: gk = p.grad.data param_state = self.state[p] gktbl = param_state['gktbl'] gavg = param_state['gavg'] param_state['gktbl_old'] = gktbl.clone() param_state['gavg_old'] = gavg.clone() def recalibrate_start(self): """ Part of the recalibration pass with SVRG. Stores the gradients for later use. """ self.epoch += 1 self.initialize() self.recalibration_i = 0 # Write out any logging stats if needed if len(self.gradient_variances) > 0: fname = 'stats/{}variance_epoch{}.pkl'.format(self.test_name, self.epoch) with open(fname, 'wb') as output: pickle.dump({ 'gradient_variances': self.gradient_variances, 'vr_step_variances': self.vr_step_variances, 'batch_indices': self.batch_indices, 'iterate_distances': self.iterate_distances, 'epoch': self.epoch, }, output) self.gradient_variances = [] self.vr_step_variances = [] self.batch_indices = [] print("logging pass diagnostics saved to {}".format(fname)) if self.epoch >= self.vr_from_epoch: for group in self.param_groups: for p in group['params']: param_state = self.state[p] param_state['gavg'].zero_() param_state['m2'].zero_() # xk is changed to the running_x p.data.zero_().add_(param_state['running_x']) param_state['tilde_x'] = p.data.clone() else: logging.info("Skipping recalibration as epoch {} not >= {}".format( self.epoch, self.vr_from_epoch)) def recalibrate(self, batch_id, closure): """ Part of the recalibration pass with SVRG. Stores the gradients for later use. """ loss = closure() #print("recal loss:", loss) m = self.nbatches self.recalibration_i += 1 if self.epoch >= self.vr_from_epoch: for group in self.param_groups: for p in group['params']: if p.grad is None: print("grad none") pdb.set_trace() continue gk = p.grad.data.double() param_state = self.state[p] gktbl = param_state['gktbl'] gavg = param_state['gavg'] m2 = param_state['m2'] #pdb.set_trace() # Online mean/variance calcuation from wikipedia delta = gk - gavg gavg.add_(1.0/self.recalibration_i, delta) delta2 = gk - gavg m2.add_((delta*delta2).type_as(m2)) param_state['running_mean'].zero_().add_(gavg) param_state['running_cov'].zero_().add_(1.0/self.nbatches, m2.double()) ######### gktbl[batch_id, :] = p.grad.data.cpu().clone() return loss def epoch_diagnostics(self, train_loss, train_err, test_loss, test_err): """ Called after recalibrate, saves stats out to disk. """ m = self.nbatches logging.info("Epoch diagnostics computation") layernum = 0 layer_gradient_norm_sqs = [] gavg_norm_acum = 0.0 gavg_acum = [] for group in self.param_groups: for p in group['params']: layer_gradient_norm_sqs.append([]) gavg = self.state[p]['gavg'].cpu() gavg_acum.append(gavg.numpy()) gavg_norm_acum += gavg.norm()**2 #torch.dot(gavg, gavg) layernum += 1 gradient_norm_sqs = [] vr_step_variance = [] cos_acums = [] variances = [] for batch_id in range(m): norm_acum = 0.0 ginorm_acum = 0.0 vr_acum = 0.0 layernum = 0 cos_acum = 0.0 var_acum = 0.0 for group in self.param_groups: for p in group['params']: param_state = self.state[p] gktbl = param_state['gktbl'] gavg = param_state['gavg'].type_as(p.data).cpu() gi = gktbl[batch_id, :] var_norm_sq = (gi-gavg).norm()**2 #torch.dot(gi-gavg, gi-gavg) norm_acum += var_norm_sq ginorm_acum += gi.norm()**2 #torch.dot(gi, gi) layer_gradient_norm_sqs[layernum].append(var_norm_sq) gktbl_old = param_state['gktbl_old'] gavg_old = param_state['gavg_old'].type_as(p.data).cpu() gi_old = gktbl_old[batch_id, :] #pdb.set_trace() vr_step = gi - gi_old + gavg_old vr_acum += (vr_step - gavg).norm()**2 #torch.dot(vr_step - gavg, vr_step - gavg) cos_acum += torch.sum(gavg*gi) var_acum += (gi - gavg).norm()**2 layernum += 1 gradient_norm_sqs.append(norm_acum) vr_step_variance.append(vr_acum) cosim = cos_acum/math.sqrt(ginorm_acum*gavg_norm_acum) #pdb.set_trace() cos_acums.append(cosim) variances.append(var_acum) variance = sum(variances)/len(variances) print("mean cosine: {}".format(sum(cos_acums)/len(cos_acums))) #pdb.set_trace() with open('stats/{}fastdiagnostics_epoch{}.pkl'.format(self.test_name, self.epoch), 'wb') as output: pickle.dump({ 'train_loss': train_loss, 'train_err': train_err, 'test_loss': test_loss, 'test_err': test_err, 'epoch': self.epoch, #'layer_gradient_norm_sqs': layer_gradient_norm_sqs, #'gradient_norm_sqs': gradient_norm_sqs, #'vr_step_variance': vr_step_variance, #'cosine_distances': cos_acums, #'variances': variances, 'variance': variance, #'gavg_norm': gavg_norm_acum, #'gavg': gavg_acum, #'iterate_distances': self.inrun_iterate_distances, #'grad_distances': self.inrun_grad_distances, }, output) print("Epoch diagnostics saved") #pdb.set_trace() self.inrun_iterate_distances = [] self.inrun_grad_distances = [] def logging_pass_start(self): self.logging_evals = 0 for group in self.param_groups: for p in group['params']: param_state = self.state[p] logging_gavg = param_state['logging_gavg'] logging_gavg.zero_() def logging_pass(self, batch_id, closure): loss = closure() m = self.nbatches for group in self.param_groups: for p in group['params']: gk = p.grad.data param_state = self.state[p] logging_gktbl = param_state['logging_gktbl'] logging_gavg = param_state['logging_gavg'] logging_gavg.add_(1.0/m, gk.double()) logging_gktbl[batch_id, :] = gk.cpu().clone() self.logging_evals += 1 return loss def logging_pass_end(self, batch_idx): m = self.nbatches logging.info("logging diagnostics computation") gradient_sqs = [] vr_step_sqs = [] forth_sqs = [] dist_sq_acum = 0.0 for group in self.param_groups: for p in group['params']: param_state = self.state[p] tilde_x = param_state['tilde_x'] iterate_diff = p.data - tilde_x dist_sq_acum += iterate_diff.norm()**2 #torch.dot(iterate_diff,iterate_diff) dist = math.sqrt(dist_sq_acum) for batch_id in range(m): grad_norm_acum = 0.0 vr_norm_acum = 0.0 forth_acum = 0.0 for group in self.param_groups: for p in group['params']: param_state = self.state[p] gktbl = param_state['gktbl'] gavg = param_state['gavg'].type_as(p.data).cpu() gi = gktbl[batch_id, :].type_as(p.data).cpu() # Logging versions are at current location xk, # compared to gavg/tktbl which are at xtilde logging_gktbl = param_state['logging_gktbl'] logging_gavg = param_state['logging_gavg'].type_as(p.data).cpu() logging_gi = logging_gktbl[batch_id, :].type_as(p.data).cpu() vr_step = (logging_gi - gi + gavg) - logging_gavg gi_step = logging_gi - logging_gavg grad_norm_acum += gi_step.pow(2.0).sum().item() vr_norm_acum += vr_step.pow(2.0).sum().item() forth_acum += gi_step.pow(2.0).sum().item() gradient_sqs.append(grad_norm_acum) vr_step_sqs.append(vr_norm_acum) forth_sqs.append(forth_acum**2) # Compute variance numbers gradient_variance = sum(gradient_sqs)/m fourth_moment = sum(forth_sqs)/m - gradient_variance**2 vr_step_variance = sum(vr_step_sqs)/m logging.info("gradient variance: {} vr: {}, ratio vr/g: {}".format( gradient_variance, vr_step_variance, vr_step_variance/gradient_variance)) logging.info(f"forth: {fourth_moment} relative std: {math.sqrt(fourth_moment)/gradient_variance} rel SE: {math.sqrt(fourth_moment/m)/gradient_variance}") logging.info("self.logging_evals: {}".format(self.logging_evals)) #pdb.set_trace() self.gradient_variances.append(gradient_variance) self.vr_step_variances.append(vr_step_variance) self.batch_indices.append(batch_idx) self.iterate_distances.append(dist) def step(self, batch_id, closure): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = closure() dist_sq_acum = 0.0 grad_dist_sq_acum = 0.0 #print("step loss: ", loss) for group in self.param_groups: momentum = group['momentum'] weight_decay = group['weight_decay'] learning_rate = group['lr'] for p in group['params']: if p.grad is None: continue gk = p.grad.data param_state = self.state[p] gktbl = param_state['gktbl'] gavg = param_state['gavg'].type_as(p.data) tilde_x = param_state['tilde_x'] if momentum != 0: buf = param_state['momentum_buffer'] ######### if self.epoch < self.vr_from_epoch: vr_gradient = gk.clone() # Just do sgd steps else: gi = gktbl[batch_id, :].cuda() vr_gradient = gk.clone().sub_(gi - gavg) # Some diagnostics iterate_diff = p.data - tilde_x #pdb.set_trace() dist_sq_acum += iterate_diff.norm()**2 #torch.dot(iterate_diff,iterate_diff) grad_diff = gi - gk grad_dist_sq_acum += grad_diff.norm()**2 #torch.dot(grad_diff,grad_diff) if weight_decay != 0: vr_gradient.add_(weight_decay, p.data) if momentum != 0: dampening = 0.0 vr_gradient = buf.mul_(momentum).add_(1 - dampening, vr_gradient) # Take step. p.data.add_(-learning_rate, vr_gradient) # Update running iterate mean: param_state['running_x'].mul_(self.running_interp).add_(1-self.running_interp, p.data) # track number of minibatches seen self.batches_processed += 1 dist = math.sqrt(dist_sq_acum) grad_dist = math.sqrt(grad_dist_sq_acum) self.inrun_iterate_distances.append(dist) self.inrun_grad_distances.append(grad_dist) return loss
deep-variance-reduction-main
torch_svrg.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import torch.optim as optim import torch_svrg import recompute_svrg import scsg def optimizer(model, args): print("Using", args.method) if args.method == "sgd": optimizer = optim.SGD(model.parameters(), lr=args.lr, weight_decay=args.decay, momentum=args.momentum) elif args.method == "svrg": optimizer = torch_svrg.SVRG(model.parameters(), args=args, lr=args.lr, nbatches=args.nbatches, momentum=args.momentum, weight_decay=args.decay) elif args.method == "recompute_svrg": optimizer = recompute_svrg.RecomputeSVRG(model.parameters(), lr=args.lr, nbatches=args.nbatches, model=model, vr_bn_at_recalibration=args.vr_bn_at_recalibration, vr_from_epoch=args.vr_from_epoch, momentum=args.momentum, weight_decay=args.decay) elif args.method == "scsg": optimizer = scsg.SCSG(model.parameters(), args=args, lr=args.lr, nbatches=args.nbatches, model=model, vr_bn_at_recalibration=args.vr_bn_at_recalibration, vr_from_epoch=args.vr_from_epoch, momentum=args.momentum, weight_decay=args.decay) else: raise Exception("Optimizer not recognised:", args.method) return optimizer
deep-variance-reduction-main
optimizers.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import torch import torch.nn as nn __all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152', 'resnext50_32x4d', 'resnext101_32x8d', 'wide_resnet50_2', 'wide_resnet101_2'] model_urls = { 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth', 'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth', 'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth', 'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth', 'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth', 'resnext50_32x4d': 'https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pth', 'resnext101_32x8d': 'https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pth', 'wide_resnet50_2': 'https://download.pytorch.org/models/wide_resnet50_2-95faca4d.pth', 'wide_resnet101_2': 'https://download.pytorch.org/models/wide_resnet101_2-32ee1156.pth', } def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1): """3x3 convolution with padding""" return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=dilation, groups=groups, bias=False, dilation=dilation) def conv1x1(in_planes, out_planes, stride=1): """1x1 convolution""" return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False) class BasicBlock(nn.Module): expansion = 1 __constants__ = ['downsample'] def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1, base_width=64, dilation=1, norm_layer=None): super(BasicBlock, self).__init__() if norm_layer is None: norm_layer = nn.BatchNorm2d if groups != 1 or base_width != 64: raise ValueError('BasicBlock only supports groups=1 and base_width=64') if dilation > 1: raise NotImplementedError("Dilation > 1 not supported in BasicBlock") # Both self.conv1 and self.downsample layers downsample the input when stride != 1 self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = norm_layer(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) self.bn2 = norm_layer(planes) self.downsample = downsample self.stride = stride def forward(self, x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) if self.downsample is not None: identity = self.downsample(x) out += identity out = self.relu(out) return out class Bottleneck(nn.Module): expansion = 4 __constants__ = ['downsample'] def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1, base_width=64, dilation=1, norm_layer=None): super(Bottleneck, self).__init__() if norm_layer is None: norm_layer = nn.BatchNorm2d width = int(planes * (base_width / 64.)) * groups # Both self.conv2 and self.downsample layers downsample the input when stride != 1 self.conv1 = conv1x1(inplanes, width) self.bn1 = norm_layer(width) self.conv2 = conv3x3(width, width, stride, groups, dilation) self.bn2 = norm_layer(width) self.conv3 = conv1x1(width, planes * self.expansion) self.bn3 = norm_layer(planes * self.expansion) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride def forward(self, x): identity = x out = self.conv1(x) out = self.bn1(out) out = self.relu(out) out = self.conv2(out) out = self.bn2(out) out = self.relu(out) out = self.conv3(out) out = self.bn3(out) if self.downsample is not None: identity = self.downsample(x) out += identity out = self.relu(out) return out class ResNet(nn.Module): def __init__(self, block, layers, num_classes=1000, zero_init_residual=False, groups=1, width_per_group=64, replace_stride_with_dilation=None, norm_layer=None): super(ResNet, self).__init__() if norm_layer is None: norm_layer = nn.BatchNorm2d self._norm_layer = norm_layer self.inplanes = 64 self.dilation = 1 if replace_stride_with_dilation is None: # each element in the tuple indicates if we should replace # the 2x2 stride with a dilated convolution instead replace_stride_with_dilation = [False, False, False] if len(replace_stride_with_dilation) != 3: raise ValueError("replace_stride_with_dilation should be None " "or a 3-element tuple, got {}".format(replace_stride_with_dilation)) self.groups = groups self.base_width = width_per_group self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = norm_layer(self.inplanes) self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0]) self.layer2 = self._make_layer(block, 128, layers[1], stride=2, dilate=replace_stride_with_dilation[0]) self.layer3 = self._make_layer(block, 256, layers[2], stride=2, dilate=replace_stride_with_dilation[1]) self.layer4 = self._make_layer(block, 512, layers[3], stride=2, dilate=replace_stride_with_dilation[2]) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(512 * block.expansion, num_classes) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) # Zero-initialize the last BN in each residual branch, # so that the residual branch starts with zeros, and each residual block behaves like an identity. # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677 if zero_init_residual: for m in self.modules(): if isinstance(m, Bottleneck): nn.init.constant_(m.bn3.weight, 0) elif isinstance(m, BasicBlock): nn.init.constant_(m.bn2.weight, 0) def _make_layer(self, block, planes, blocks, stride=1, dilate=False): norm_layer = self._norm_layer downsample = None previous_dilation = self.dilation if dilate: self.dilation *= stride stride = 1 if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( conv1x1(self.inplanes, planes * block.expansion, stride), norm_layer(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample, self.groups, self.base_width, previous_dilation, norm_layer)) self.inplanes = planes * block.expansion for _ in range(1, blocks): layers.append(block(self.inplanes, planes, groups=self.groups, base_width=self.base_width, dilation=self.dilation, norm_layer=norm_layer)) return nn.Sequential(*layers) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu(x) x = self.maxpool(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.avgpool(x) x = torch.flatten(x, 1) x = self.fc(x) return x def _resnet(arch, block, layers, pretrained, progress, **kwargs): model = ResNet(block, layers, **kwargs) return model def resnet18(pretrained=False, progress=True, **kwargs): r"""ResNet-18 model from `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_ Args: pretrained (bool): If True, returns a model pre-trained on ImageNet progress (bool): If True, displays a progress bar of the download to stderr """ return _resnet('resnet18', BasicBlock, [2, 2, 2, 2], pretrained, progress, **kwargs) def resnet34(pretrained=False, progress=True, **kwargs): r"""ResNet-34 model from `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_ Args: pretrained (bool): If True, returns a model pre-trained on ImageNet progress (bool): If True, displays a progress bar of the download to stderr """ return _resnet('resnet34', BasicBlock, [3, 4, 6, 3], pretrained, progress, **kwargs) def resnet50(pretrained=False, progress=True, **kwargs): r"""ResNet-50 model from `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_ Args: pretrained (bool): If True, returns a model pre-trained on ImageNet progress (bool): If True, displays a progress bar of the download to stderr """ return _resnet('resnet50', Bottleneck, [3, 4, 6, 3], pretrained, progress, **kwargs) def resnet101(pretrained=False, progress=True, **kwargs): r"""ResNet-101 model from `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_ Args: pretrained (bool): If True, returns a model pre-trained on ImageNet progress (bool): If True, displays a progress bar of the download to stderr """ return _resnet('resnet101', Bottleneck, [3, 4, 23, 3], pretrained, progress, **kwargs) def resnet152(pretrained=False, progress=True, **kwargs): r"""ResNet-152 model from `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_ Args: pretrained (bool): If True, returns a model pre-trained on ImageNet progress (bool): If True, displays a progress bar of the download to stderr """ return _resnet('resnet152', Bottleneck, [3, 8, 36, 3], pretrained, progress, **kwargs) def resnext50_32x4d(pretrained=False, progress=True, **kwargs): r"""ResNeXt-50 32x4d model from `"Aggregated Residual Transformation for Deep Neural Networks" <https://arxiv.org/pdf/1611.05431.pdf>`_ Args: pretrained (bool): If True, returns a model pre-trained on ImageNet progress (bool): If True, displays a progress bar of the download to stderr """ kwargs['groups'] = 32 kwargs['width_per_group'] = 4 return _resnet('resnext50_32x4d', Bottleneck, [3, 4, 6, 3], pretrained, progress, **kwargs) def resnext101_32x8d(pretrained=False, progress=True, **kwargs): r"""ResNeXt-101 32x8d model from `"Aggregated Residual Transformation for Deep Neural Networks" <https://arxiv.org/pdf/1611.05431.pdf>`_ Args: pretrained (bool): If True, returns a model pre-trained on ImageNet progress (bool): If True, displays a progress bar of the download to stderr """ kwargs['groups'] = 32 kwargs['width_per_group'] = 8 return _resnet('resnext101_32x8d', Bottleneck, [3, 4, 23, 3], pretrained, progress, **kwargs) def wide_resnet50_2(pretrained=False, progress=True, **kwargs): r"""Wide ResNet-50-2 model from `"Wide Residual Networks" <https://arxiv.org/pdf/1605.07146.pdf>`_ The model is the same as ResNet except for the bottleneck number of channels which is twice larger in every block. The number of channels in outer 1x1 convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048 channels, and in Wide ResNet-50-2 has 2048-1024-2048. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet progress (bool): If True, displays a progress bar of the download to stderr """ kwargs['width_per_group'] = 64 * 2 return _resnet('wide_resnet50_2', Bottleneck, [3, 4, 6, 3], pretrained, progress, **kwargs) def wide_resnet101_2(pretrained=False, progress=True, **kwargs): r"""Wide ResNet-101-2 model from `"Wide Residual Networks" <https://arxiv.org/pdf/1605.07146.pdf>`_ The model is the same as ResNet except for the bottleneck number of channels which is twice larger in every block. The number of channels in outer 1x1 convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048 channels, and in Wide ResNet-50-2 has 2048-1024-2048. Args: pretrained (bool): If True, returns a model pre-trained on ImageNet progress (bool): If True, displays a progress bar of the download to stderr """ kwargs['width_per_group'] = 64 * 2 return _resnet('wide_resnet101_2', Bottleneck, [3, 4, 23, 3], pretrained, progress, **kwargs)
deep-variance-reduction-main
resnext.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import os import torch import torch.nn as nn import torch.nn.functional as F from torchvision import datasets, transforms from torch.utils.data.sampler import RandomSampler import torchvision import torchvision.transforms as transforms import logging from torch.autograd import Variable import torch.utils import torch.utils.data import torch.utils.data.distributed import torch.distributed as dist from cifar_wrapper import CIFAR10_Wrapper from vr_sampler import VRSampler import UpdatedDataLoader import UpdatedDataLoaderMult import resnet import pdb import densenet import resnext from imagenet_wrapper import ImagenetWrapper def load(args): print("Problem:", args.problem) if args.problem == "cifar10": return cifar10(args) elif args.problem == "imagenet": return imagenet(args) else: raise Exception("Unrecognised problem:", args.problem) def cifar10(args): data_dir = os.path.expanduser('~/data') kwargs = {'num_workers': 0, 'pin_memory': True} if args.cuda else {} transform = transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.RandomCrop(32, padding=4), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ]) # We don't do the random transforms at test time. transform_test = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ]) logging.info("Loading training dataset") if (args.method.endswith("svrg") or args.method == "scsg") and args.transform_locking and args.opt_vr: train_dataset = CIFAR10_Wrapper( root=data_dir, train=True, download=True, transform=transform) else: train_dataset = torchvision.datasets.CIFAR10( root=data_dir, train=True, download=True, transform=transform) if args.method.endswith("svrg") and args.opt_vr: if args.method == "saga": raise Exception("vr sampler currently doesn't support saga") logging.info("VR Sampler with order=perm") sampler = VRSampler(order="perm", batch_size=args.batch_size, dataset_size=len(train_dataset)) train_loader = UpdatedDataLoader.DataLoader( train_dataset, batch_sampler=sampler, **kwargs) else: sampler = RandomSampler(train_dataset) train_loader = torch.utils.data.DataLoader( train_dataset, sampler=sampler, batch_size=args.batch_size, **kwargs) args.nbatches = len(sampler) logging.info("Loading test dataset") test_dataset = torchvision.datasets.CIFAR10( root=data_dir, train=False, download=True, transform=transform_test) test_loader = torch.utils.data.DataLoader( test_dataset, batch_size=args.batch_size, shuffle=False, **kwargs) nonlinearity = F.relu class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(3, 6, 5) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) logging.info("Initializing fully connected layers") self.fc1 = nn.Linear(16 * 5 * 5, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) if args.batchnorm: logging.info("Using batchnorm") self.bn1 = nn.BatchNorm2d(6) self.bn2 = nn.BatchNorm2d(16) self.bn3 = nn.BatchNorm1d(120) self.bn4 = nn.BatchNorm1d(84) logging.info("initialized") def forward(self, x): x = self.conv1(x) if args.batchnorm: x = self.bn1(x) x = nonlinearity (x) x = self.pool(x) #pdb.set_trace() x = self.conv2(x) if args.batchnorm: x = self.bn2(x) x = nonlinearity (x) x = self.pool(x) x = x.view(-1, 16 * 5 * 5) x = self.fc1(x) if args.batchnorm: x = self.bn3(x) x = nonlinearity (x) x = self.fc2(x) if args.batchnorm: x = self.bn4(x) x = nonlinearity (x) x = self.fc3(x) return x logging.info("Loading architecture") if args.architecture == "default": logging.info("default architecture") model = Net() elif args.architecture == "resnet110": model = resnet.ResNet110(batchnorm=args.batchnorm, nonlinearity=nonlinearity) elif args.architecture == "resnet-small": model = resnet.ResNetSmall(batchnorm=args.batchnorm, nonlinearity=nonlinearity) elif args.architecture == "densenet-40-36": model = densenet.densenet(depth=40, growthRate=36, batchnorm=args.batchnorm, nonlinearity=nonlinearity) model = torch.nn.DataParallel(model) else: raise Exception("architecture not recognised:", args.architecture) model.sampler = sampler return train_loader, test_loader, model, train_dataset def imagenet(args): kwargs = {'num_workers': 32, 'pin_memory': True} if args.cuda else {} normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) lock_transforms = (args.method.endswith("svrg")) and args.transform_locking and args.opt_vr logging.info("Loading training dataset") train_dir = "/datasets01_101/imagenet_full_size/061417/train" logging.info("Data ...") train_dataset = ImagenetWrapper(train_dir, lock_transforms=lock_transforms) logging.info("Imagenet Wrapper created") logging.info("VR Sampler with order=perm") sampler = VRSampler(order="perm", batch_size=args.batch_size, dataset_size=len(train_dataset)) train_loader = UpdatedDataLoaderMult.DataLoader( train_dataset, batch_sampler=sampler, worker_init_fn=train_dataset.child_initialize, **kwargs) #worker_init_fn logging.info("Train Loader created, batches: {}".format(len(train_loader))) test_loader = torch.utils.data.DataLoader( datasets.ImageFolder("/datasets01_101/imagenet_full_size/061417/val", transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize, ])), batch_size=args.batch_size, shuffle=False, **kwargs) args.nbatches = len(train_loader) logging.info("Initializing model") if args.architecture == "resnet18": model = torchvision.models.resnet.resnet18() elif args.architecture == "resnet50": model = torchvision.models.resnet.resnet50() elif args.architecture == "resnext101_32x8d": model = resnext.resnext101_32x8d() else: raise Exception("Architecture not supported for imagenet") logging.info("Lifting model to DataParallel") model = torch.nn.DataParallel(model).cuda() # Use multiple gpus model.sampler = sampler return train_loader, test_loader, model, train_dataset
deep-variance-reduction-main
problems.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import numbers import math import random from PIL import Image, ImageOps, ImageEnhance import numpy as np import numbers import types import collections import warnings try: import accimage except ImportError: accimage = None def _is_pil_image(img): if accimage is not None: return isinstance(img, (Image.Image, accimage.Image)) else: return isinstance(img, Image.Image) import torch class RandomHorizontalFlip(object): """Horizontally flip the given PIL Image randomly with a probability of 0.5.""" def transform(self, img, r): if r < 0.5: return hflip(img) return img class RandomResizedCrop(object): """Crop the given PIL Image to random size and aspect ratio. A crop of random size (default: of 0.08 to 1.0) of the original size and a random aspect ratio (default: of 3/4 to 4/3) of the original aspect ratio is made. This crop is finally resized to given size. This is popularly used to train the Inception networks. Args: size: expected output size of each edge scale: range of size of the origin size cropped ratio: range of aspect ratio of the origin aspect ratio cropped interpolation: Default: PIL.Image.BILINEAR """ def __init__(self, size, scale=(0.08, 1.0), ratio=(3. / 4., 4. / 3.), interpolation=Image.BILINEAR): self.size = (size, size) self.interpolation = interpolation self.scale = scale self.ratio = ratio @staticmethod def get_params_from_random(img, scale, ratio, r1, r2, r3, r4, r5): """Get parameters for ``crop`` for a random sized crop. Args: img (PIL Image): Image to be cropped. scale (tuple): range of size of the origin size cropped ratio (tuple): range of aspect ratio of the origin aspect ratio cropped Returns: tuple: params (i, j, h, w) to be passed to ``crop`` for a random sized crop. """ for attempt in range(6): area = img.size[0] * img.size[1] #target_area = random.uniform(*scale) * area target_area = (r1*(scale[1] - scale[0]) + scale[0]) * area #aspect_ratio = random.uniform(*ratio) aspect_ratio = r2*(ratio[1] - ratio[0]) + ratio[0] w = int(round(math.sqrt(target_area * aspect_ratio))) h = int(round(math.sqrt(target_area / aspect_ratio))) if r3 < 0.5: w, h = h, w if w <= img.size[0] and h <= img.size[1]: # randint is inclusive i = int(r4*(img.size[1] - h + 1)) #i = random.randint(0, img.size[1] - h) j = int(r5*(img.size[0] - w + 1)) #j = random.randint(0, img.size[0] - w) return i, j, h, w # Rotate through them. tmp = r1 r1 = r2 r2 = r3 r3 = r4 r4 = r5 r5 = tmp #print("Attempts failed") # Fallback w = min(img.size[0], img.size[1]) i = (img.size[1] - w) // 2 j = (img.size[0] - w) // 2 return i, j, w, w def transform(self, img, r1, r2, r3, r4, r5): #i, j, h, w, size, interpolation = r i, j, h, w = self.get_params_from_random(img, self.scale, self.ratio, r1, r2, r3, r4, r5) return resized_crop(img, i, j, h, w, self.size, self.interpolation) # @staticmethod # def delegate(i, j, h, w, size, interpolation): # return (lambda img: resized_crop(img, i, j, h, w, size, interpolation)) #def __call__(self, img_prev): # i, j, h, w = self.get_params(img_prev, self.scale, self.ratio) # return () #return self.delegate(i, j, h, w, self.size, self.interpolation) def resized_crop(img, i, j, h, w, size, interpolation=Image.BILINEAR): """Crop the given PIL Image and resize it to desired size. Notably used in RandomResizedCrop. Args: img (PIL Image): Image to be cropped. i: Upper pixel coordinate. j: Left pixel coordinate. h: Height of the cropped image. w: Width of the cropped image. size (sequence or int): Desired output size. Same semantics as ``scale``. interpolation (int, optional): Desired interpolation. Default is ``PIL.Image.BILINEAR``. Returns: PIL Image: Cropped image. """ assert _is_pil_image(img), 'img should be PIL Image' img = crop(img, i, j, h, w) img = resize(img, size, interpolation) return img def crop(img, i, j, h, w): """Crop the given PIL Image. Args: img (PIL Image): Image to be cropped. i: Upper pixel coordinate. j: Left pixel coordinate. h: Height of the cropped image. w: Width of the cropped image. Returns: PIL Image: Cropped image. """ if not _is_pil_image(img): raise TypeError('img should be PIL Image. Got {}'.format(type(img))) return img.crop((j, i, j + w, i + h)) def resize(img, size, interpolation=Image.BILINEAR): """Resize the input PIL Image to the given size. Args: img (PIL Image): Image to be resized. size (sequence or int): Desired output size. If size is a sequence like (h, w), the output size will be matched to this. If size is an int, the smaller edge of the image will be matched to this number maintaing the aspect ratio. i.e, if height > width, then image will be rescaled to (size * height / width, size) interpolation (int, optional): Desired interpolation. Default is ``PIL.Image.BILINEAR`` Returns: PIL Image: Resized image. """ if not _is_pil_image(img): raise TypeError('img should be PIL Image. Got {}'.format(type(img))) if not (isinstance(size, int) or (isinstance(size, collections.Iterable) and len(size) == 2)): raise TypeError('Got inappropriate size arg: {}'.format(size)) if isinstance(size, int): w, h = img.size if (w <= h and w == size) or (h <= w and h == size): return img if w < h: ow = size oh = int(size * h / w) return img.resize((ow, oh), interpolation) else: oh = size ow = int(size * w / h) return img.resize((ow, oh), interpolation) else: return img.resize(size[::-1], interpolation) def hflip(img): """Horizontally flip the given PIL Image. Args: img (PIL Image): Image to be flipped. Returns: PIL Image: Horizontall flipped image. """ if not _is_pil_image(img): raise TypeError('img should be PIL Image. Got {}'.format(type(img))) return img.transpose(Image.FLIP_LEFT_RIGHT) def to_tensor(pic): """Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor. See ``ToTensor`` for more details. Args: pic (PIL Image or numpy.ndarray): Image to be converted to tensor. Returns: Tensor: Converted image. """ if not(_is_pil_image(pic) or _is_numpy_image(pic)): raise TypeError('pic should be PIL Image or ndarray. Got {}'.format(type(pic))) if isinstance(pic, np.ndarray): # handle numpy array img = torch.from_numpy(pic.transpose((2, 0, 1))) # backward compatibility return img.float().div(255) if accimage is not None and isinstance(pic, accimage.Image): nppic = np.zeros([pic.channels, pic.height, pic.width], dtype=np.float32) pic.copyto(nppic) return torch.from_numpy(nppic) # handle PIL Image if pic.mode == 'I': img = torch.from_numpy(np.array(pic, np.int32, copy=False)) elif pic.mode == 'I;16': img = torch.from_numpy(np.array(pic, np.int16, copy=False)) else: img = torch.ByteTensor(torch.ByteStorage.from_buffer(pic.tobytes())) # PIL image mode: 1, L, P, I, F, RGB, YCbCr, RGBA, CMYK if pic.mode == 'YCbCr': nchannel = 3 elif pic.mode == 'I;16': nchannel = 1 else: nchannel = len(pic.mode) img = img.view(pic.size[1], pic.size[0], nchannel) # put it from HWC to CHW format # yikes, this transpose takes 80% of the loading time/CPU img = img.transpose(0, 1).transpose(0, 2).contiguous() if isinstance(img, torch.ByteTensor): return img.float().div(255) else: return img
deep-variance-reduction-main
caching_transforms.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. '''ResNet in PyTorch. from https://github.com/kuangliu/pytorch-cifar/blob/master/models/resnet.py based on https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py BasicBlock and Bottleneck module is from the original ResNet paper: [1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun Deep Residual Learning for Image Recognition. arXiv:1512.03385 PreActBlock and PreActBottleneck module is from the later paper: [2] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun Identity Mappings in Deep Residual Networks. arXiv:1603.05027 ''' import torch import torch.nn as nn import torch.nn.functional as F from torch.autograd import Variable def conv3x3(in_planes, out_planes, stride=1): return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) class PreActBlock(nn.Module): '''Pre-activation version of the BasicBlock.''' expansion = 1 def __init__(self, in_planes, planes, stride=1, batchnorm=True, nonlinearity=None): super(PreActBlock, self).__init__() self.batchnorm = batchnorm if batchnorm: self.bn1 = nn.BatchNorm2d(in_planes) self.bn2 = nn.BatchNorm2d(planes) self.conv1 = conv3x3(in_planes, planes, stride) self.conv2 = conv3x3(planes, planes) self.nonlinearity = nonlinearity if stride != 1 or in_planes != self.expansion*planes: self.shortcut = nn.Sequential( nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False) ) def forward(self, x): if self.batchnorm: out = self.bn1(x) else: out = x out = self.nonlinearity(out) shortcut = self.shortcut(out) if hasattr(self, 'shortcut') else x out = self.conv1(out) if self.batchnorm: out = self.bn2(out) out = self.nonlinearity(out) out = self.conv2(out) out += shortcut return out class Bottleneck(nn.Module): expansion = 4 def __init__(self, in_planes, planes, stride=1, batchnorm=True, nonlinearity=None): super(Bottleneck, self).__init__() self.batchnorm = batchnorm if batchnorm: self.bn1 = nn.BatchNorm2d(planes) self.bn2 = nn.BatchNorm2d(planes) self.bn3 = nn.BatchNorm2d(self.expansion*planes) self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False) self.nonlinearity = nonlinearity self.shortcut = nn.Sequential() if stride != 1 or in_planes != self.expansion*planes: if batchnorm: self.shortcut = nn.Sequential( nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(self.expansion*planes) ) else: self.shortcut = nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False) def forward(self, x): out = self.conv1(x) if self.batchnorm: out = self.bn1(out) out = self.nonlinearity(out) out = self.conv2(out) if self.batchnorm: out = self.bn2(out) out = self.nonlinearity(out) out = self.conv3(out) if self.batchnorm: out = self.bn3(out) out += self.shortcut(x) out = self.nonlinearity(out) return out class PreActBottleneck(nn.Module): '''Pre-activation version of the original Bottleneck module.''' expansion = 4 def __init__(self, in_planes, planes, stride=1, batchnorm=True, nonlinearity=None): super(PreActBottleneck, self).__init__() self.batchnorm = batchnorm if batchnorm: self.bn1 = nn.BatchNorm2d(in_planes) self.bn2 = nn.BatchNorm2d(planes) self.bn3 = nn.BatchNorm2d(planes) self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.conv3 = nn.Conv2d(planes, self.expansion*planes, kernel_size=1, bias=False) self.nonlinearity = nonlinearity if stride != 1 or in_planes != self.expansion*planes: self.shortcut = nn.Sequential( nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False) ) def forward(self, x): if self.batchnorm: out = self.bn1(x) else: out = x out = self.nonlinearity(out) shortcut = self.shortcut(out) if hasattr(self, 'shortcut') else x out = self.conv1(out) if self.batchnorm: out = self.bn2(out) out = self.conv2(self.nonlinearity(out)) if self.batchnorm: out = self.bn3(out) out = self.conv3(self.nonlinearity(out)) out += shortcut return out class ResNetImageNet(nn.Module): def __init__(self, block, num_blocks, num_classes=10, batchnorm=True, nonlinearity=None): super(ResNetImageNet, self).__init__() self.batchnorm = batchnorm self.in_planes = 64 self.conv1 = conv3x3(3,64) if batchnorm: self.bn1 = nn.BatchNorm2d(64) self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1) self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2) self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2) self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2) self.linear = nn.Linear(512*block.expansion, num_classes) self.nonlinearity = nonlinearity def _make_layer(self, block, planes, num_blocks, stride): strides = [stride] + [1]*(num_blocks-1) layers = [] for stride in strides: layers.append(block( self.in_planes, planes, stride, batchnorm=self.batchnorm, nonlinearity=self.nonlinearity)) self.in_planes = planes * block.expansion return nn.Sequential(*layers) def forward(self, x): out = self.conv1(x) if self.batchnorm: out = self.bn1(out) out = self.nonlinearity(out) out = self.layer1(out) out = self.layer2(out) out = self.layer3(out) out = self.layer4(out) out = F.avg_pool2d(out, 4) out = out.view(out.size(0), -1) out = self.linear(out) return out class ResNetCifar(nn.Module): def __init__(self, block, blocks_per=3, num_classes=10, batchnorm=True, nonlinearity=None, in_planes=16): super(ResNetCifar, self).__init__() self.batchnorm = batchnorm self.in_planes = in_planes # standard resnet is 16 self.nonlinearity = nonlinearity self.conv1 = conv3x3(3, in_planes) if batchnorm: self.bn1 = nn.BatchNorm2d(in_planes) self.layer1 = self._make_layer(block, in_planes, blocks_per, stride=1) self.layer2 = self._make_layer(block, 2*in_planes, blocks_per, stride=2) self.layer3 = self._make_layer(block, 4*in_planes, blocks_per, stride=2) self.linear = nn.Linear(4*in_planes*block.expansion, num_classes) def _make_layer(self, block, planes, num_blocks, stride): strides = [stride] + [1]*(num_blocks-1) layers = [] for stride in strides: layers.append(block( self.in_planes, planes, stride, batchnorm=self.batchnorm, nonlinearity=self.nonlinearity)) self.in_planes = planes * block.expansion return nn.Sequential(*layers) def forward(self, x): out = self.conv1(x) if self.batchnorm: out = self.bn1(out) out = self.nonlinearity(out) out = self.layer1(out) out = self.layer2(out) out = self.layer3(out) #import pdb; pdb.set_trace() # torch.Size([64, 64, 8, 8]) good. outp = F.avg_pool2d(out, 8) # torch.Size([64, 64, 1, 1]) outv = outp.view(outp.size(0), -1) # after torch.Size([64, 256]) outl = self.linear(outv) # want 64x64? return outl def ResNet18(**kwargs): return ResNetImageNet(PreActBlock, [2,2,2,2], **kwargs) def ResNet50(**kwargs): return ResNetImageNet(Bottleneck, [3,4,6,3], **kwargs) def ResNetSmallWide(**kwargs): return ResNetCifar(PreActBlock, blocks_per=1, **kwargs) def ResNetSmall(**kwargs): return ResNetCifar(PreActBlock, blocks_per=3, in_planes=8, **kwargs) def ResNet(**kwargs): return ResNetCifar(PreActBlock, **kwargs) # I get out-of-memory errors for these larger ones def ResNet56(**kwargs): return ResNetCifar(PreActBlock, blocks_per=9, **kwargs) def ResNet110(**kwargs): return ResNetCifar(PreActBlock, blocks_per=18, **kwargs)
deep-variance-reduction-main
resnet.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import torch import torch.multiprocessing as multiprocessing from torch.utils.data.sampler import SequentialSampler, RandomSampler import collections import sys import traceback import threading import pdb if sys.version_info[0] == 2: import Queue as queue string_classes = basestring else: import queue string_classes = (str, bytes) _use_shared_memory = False """Whether to use shared memory in default_collate""" class BatchSampler(object): """Wraps another sampler to yield a mini-batch of indices. Args: sampler (Sampler): Base sampler. batch_size (int): Size of mini-batch. drop_last (bool): If ``True``, the sampler will drop the last batch if its size would be less than ``batch_size`` Example: >>> list(BatchSampler(range(10), batch_size=3, drop_last=False)) [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]] >>> list(BatchSampler(range(10), batch_size=3, drop_last=True)) [[0, 1, 2], [3, 4, 5], [6, 7, 8]] """ def __init__(self, sampler, batch_size, drop_last): self.sampler = sampler self.batch_size = batch_size self.drop_last = drop_last def __iter__(self): batch = [] for idx in self.sampler: batch.append(idx) if len(batch) == self.batch_size: yield batch batch = [] if len(batch) > 0 and not self.drop_last: yield batch def __len__(self): if self.drop_last: return len(self.sampler) // self.batch_size else: return (len(self.sampler) + self.batch_size - 1) // self.batch_size class ExceptionWrapper(object): "Wraps an exception plus traceback to communicate across threads" def __init__(self, exc_info): self.exc_type = exc_info[0] self.exc_msg = "".join(traceback.format_exception(*exc_info)) def _worker_loop(dataset, index_queue, data_queue, collate_fn): global _use_shared_memory _use_shared_memory = True torch.set_num_threads(1) while True: r = index_queue.get() if r is None: data_queue.put(None) break idx, batch_indices = r try: samples = collate_fn([dataset[i] for i in batch_indices]) except Exception: data_queue.put((idx, ExceptionWrapper(sys.exc_info()))) else: data_queue.put((idx, samples)) def _pin_memory_loop(in_queue, out_queue, done_event): while True: try: r = in_queue.get() except: if done_event.is_set(): return raise if r is None: break if isinstance(r[1], ExceptionWrapper): out_queue.put(r) continue idx, batch = r try: batch = pin_memory_batch(batch) except Exception: out_queue.put((idx, ExceptionWrapper(sys.exc_info()))) else: out_queue.put((idx, batch)) numpy_type_map = { 'float64': torch.DoubleTensor, 'float32': torch.FloatTensor, 'float16': torch.HalfTensor, 'int64': torch.LongTensor, 'int32': torch.IntTensor, 'int16': torch.ShortTensor, 'int8': torch.CharTensor, 'uint8': torch.ByteTensor, } def default_collate(batch): "Puts each data field into a tensor with outer dimension batch size" if torch.is_tensor(batch[0]): out = None if _use_shared_memory: # If we're in a background process, concatenate directly into a # shared memory tensor to avoid an extra copy numel = sum([x.numel() for x in batch]) storage = batch[0].storage()._new_shared(numel) out = batch[0].new(storage) return torch.stack(batch, 0, out=out) elif type(batch[0]).__module__ == 'numpy': elem = batch[0] if type(elem).__name__ == 'ndarray': return torch.stack([torch.from_numpy(b) for b in batch], 0) if elem.shape == (): # scalars py_type = float if elem.dtype.name.startswith('float') else int return numpy_type_map[elem.dtype.name](list(map(py_type, batch))) elif isinstance(batch[0], int): return torch.LongTensor(batch) elif isinstance(batch[0], float): return torch.DoubleTensor(batch) elif isinstance(batch[0], string_classes): return batch elif isinstance(batch[0], collections.Mapping): return {key: default_collate([d[key] for d in batch]) for key in batch[0]} elif isinstance(batch[0], collections.Sequence): transposed = zip(*batch) return [default_collate(samples) for samples in transposed] raise TypeError(("batch must contain tensors, numbers, dicts or lists; found {}" .format(type(batch[0])))) def pin_memory_batch(batch): if torch.is_tensor(batch): return batch.pin_memory() elif isinstance(batch, string_classes): return batch elif isinstance(batch, collections.Mapping): return {k: pin_memory_batch(sample) for k, sample in batch.items()} elif isinstance(batch, collections.Sequence): return [pin_memory_batch(sample) for sample in batch] else: return batch class DataLoaderIter(object): "Iterates once over the DataLoader's dataset, as specified by the sampler" def __init__(self, loader): self.dataset = loader.dataset self.collate_fn = loader.collate_fn self.batch_sampler = loader.batch_sampler self.num_workers = loader.num_workers self.pin_memory = loader.pin_memory self.done_event = threading.Event() self.sample_iter = iter(self.batch_sampler) if self.num_workers > 0: self.index_queue = multiprocessing.SimpleQueue() self.data_queue = multiprocessing.SimpleQueue() self.batches_outstanding = 0 self.shutdown = False self.send_idx = 0 self.rcvd_idx = 0 self.reorder_dict = {} self.workers = [ multiprocessing.Process( target=_worker_loop, args=(self.dataset, self.index_queue, self.data_queue, self.collate_fn)) for _ in range(self.num_workers)] for w in self.workers: w.daemon = True # ensure that the worker exits on process exit w.start() if self.pin_memory: in_data = self.data_queue self.data_queue = queue.Queue() self.pin_thread = threading.Thread( target=_pin_memory_loop, args=(in_data, self.data_queue, self.done_event)) self.pin_thread.daemon = True self.pin_thread.start() # prime the prefetch loop for _ in range(2 * self.num_workers): self._put_indices() def __len__(self): return len(self.batch_sampler) def __next__(self): if self.num_workers == 0: # same-process loading indices = next(self.sample_iter) # may raise StopIteration batch = self.collate_fn([self.dataset[i] for i in indices]) if self.pin_memory: batch = pin_memory_batch(batch) return batch # check if the next sample has already been generated if self.rcvd_idx in self.reorder_dict: batch = self.reorder_dict.pop(self.rcvd_idx) return self._process_next_batch(batch) if self.batches_outstanding == 0: self._shutdown_workers() raise StopIteration while True: assert (not self.shutdown and self.batches_outstanding > 0) idx, batch = self.data_queue.get() self.batches_outstanding -= 1 if idx != self.rcvd_idx: # store out-of-order samples self.reorder_dict[idx] = batch continue #pdb.set_trace() return self._process_next_batch(batch) next = __next__ # Python 2 compatibility def __iter__(self): return self def _put_indices(self): assert self.batches_outstanding < 2 * self.num_workers indices = next(self.sample_iter, None) if indices is None: return self.index_queue.put((self.send_idx, indices)) self.batches_outstanding += 1 self.send_idx += 1 def _process_next_batch(self, batch): self.rcvd_idx += 1 self._put_indices() if isinstance(batch, ExceptionWrapper): raise batch.exc_type(batch.exc_msg) return batch def __getstate__(self): # TODO: add limited pickling support for sharing an iterator # across multiple threads for HOGWILD. # Probably the best way to do this is by moving the sample pushing # to a separate thread and then just sharing the data queue # but signalling the end is tricky without a non-blocking API raise NotImplementedError("DataLoaderIterator cannot be pickled") def _shutdown_workers(self): if not self.shutdown: self.shutdown = True self.done_event.set() for _ in self.workers: self.index_queue.put(None) def __del__(self): if self.num_workers > 0: self._shutdown_workers() class DataLoader(object): """ Data loader. Combines a dataset and a sampler, and provides single- or multi-process iterators over the dataset. Arguments: dataset (Dataset): dataset from which to load the data. batch_size (int, optional): how many samples per batch to load (default: 1). shuffle (bool, optional): set to ``True`` to have the data reshuffled at every epoch (default: False). sampler (Sampler, optional): defines the strategy to draw samples from the dataset. If specified, ``shuffle`` must be False. batch_sampler (Sampler, optional): like sampler, but returns a batch of indices at a time. Mutually exclusive with batch_size, shuffle, sampler, and drop_last. num_workers (int, optional): how many subprocesses to use for data loading. 0 means that the data will be loaded in the main process (default: 0) collate_fn (callable, optional): merges a list of samples to form a mini-batch. pin_memory (bool, optional): If ``True``, the data loader will copy tensors into CUDA pinned memory before returning them. drop_last (bool, optional): set to ``True`` to drop the last incomplete batch, if the dataset size is not divisible by the batch size. If False and the size of dataset is not divisible by the batch size, then the last batch will be smaller. (default: False) """ def __init__(self, dataset, batch_size=1, shuffle=False, sampler=None, batch_sampler=None, num_workers=0, collate_fn=default_collate, pin_memory=False, drop_last=False): self.dataset = dataset self.batch_size = batch_size self.num_workers = num_workers self.collate_fn = collate_fn self.pin_memory = pin_memory self.drop_last = drop_last if batch_sampler is not None: if batch_size > 1 or shuffle or sampler is not None or drop_last: raise ValueError('batch_sampler is mutually exclusive with ' 'batch_size, shuffle, sampler, and drop_last') if sampler is not None and shuffle: raise ValueError('sampler is mutually exclusive with shuffle') if batch_sampler is None: if sampler is None: if shuffle: sampler = RandomSampler(dataset) else: sampler = SequentialSampler(dataset) batch_sampler = BatchSampler(sampler, batch_size, drop_last) self.sampler = sampler self.batch_sampler = batch_sampler def __iter__(self): return DataLoaderIter(self) def __len__(self): return len(self.batch_sampler)
deep-variance-reduction-main
UpdatedDataLoader.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from math import atan2,degrees import numpy as np #Label line with line2D label data def labelLine(line,x,label=None,align=True,**kwargs): ax = line.axes xdata = line.get_xdata() ydata = line.get_ydata() if (x < xdata[0]) or (x > xdata[-1]): print('x label location is outside data range!') return #Find corresponding y co-ordinate and angle of the line ip = 1 for i in range(len(xdata)): if x < xdata[i]: ip = i break y = ydata[ip-1] + (ydata[ip]-ydata[ip-1])*(x-xdata[ip-1])/(xdata[ip]-xdata[ip-1]) if not label: label = line.get_label() if align: #Compute the slope dx = xdata[ip] - xdata[ip-1] dy = ydata[ip] - ydata[ip-1] ang = degrees(atan2(dy,dx)) #Transform to screen co-ordinates pt = np.array([x,y]).reshape((1,2)) trans_angle = ax.transData.transform_angles(np.array((ang,)),pt)[0] else: trans_angle = 0 #Set a bunch of keyword arguments if 'color' not in kwargs: kwargs['color'] = line.get_color() if ('horizontalalignment' not in kwargs) and ('ha' not in kwargs): kwargs['ha'] = 'center' if ('verticalalignment' not in kwargs) and ('va' not in kwargs): kwargs['va'] = 'center' if 'backgroundcolor' not in kwargs: kwargs['backgroundcolor'] = ax.get_facecolor() if 'clip_on' not in kwargs: kwargs['clip_on'] = True if 'zorder' not in kwargs: kwargs['zorder'] = 2.5 # Add box with rounded corners around the label kwargs["bbox"] = dict( fill=True, facecolor=kwargs['backgroundcolor'], edgecolor=kwargs["color"], #linestyle="solid", linewidth=0.3, #None is default boxstyle="round,rounding_size=0.5,pad=0.15" ) ax.text(x,y,label,rotation=trans_angle,**kwargs) def labelLines(lines,align=True,xvals=None,**kwargs): ax = lines[0].axes labLines = [] labels = [] #Take only the lines which have labels other than the default ones for line in lines: label = line.get_label() if "_line" not in label: labLines.append(line) labels.append(label) if xvals is None: xmin,xmax = ax.get_xlim() xvals = np.linspace(xmin,xmax,len(labLines)+2)[1:-1] for line,x,label in zip(labLines,xvals,labels): labelLine(line,x,label,align,**kwargs)
deep-variance-reduction-main
reproduce/label_lines.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import os import sys import run methods = ["sgd", "recompute_svrg", "scsg"] try: pindex = int(sys.argv[1]) seed = int(sys.argv[2]) print(f"problem index {pindex}") except: pindex = 0 seed = 0 method = methods[pindex] runargs = { 'method': method, 'seed': seed, 'problem': 'imagenet', 'architecture': 'resnext101_32x8d', 'momentum': 0.9, 'lr': 0.1, 'decay': 0.0001, 'lr_reduction': "every30", 'batch_size': 256, 'epochs': 90, 'save_model': True, 'full_checkpointing': True, 'log_interval': 80, } run.run(runargs)
deep-variance-reduction-main
reproduce/reproduce_test_error_imagenet_next.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import os import sys import run archs = ['default', 'densenet-40-36'] try: pindex = int(sys.argv[1]) print(f"problem index {pindex}") except: pindex = 0 arch = archs[pindex] runargs = { 'transform_locking': True, 'problem': 'cifar10', 'architecture': arch, 'method': "svrg", 'logfname': 'reproduce-iterate-distance-{}'.format(arch), 'momentum': 0.9, 'decay': 0.0001, 'lr': 0.1, 'lr_reduction': "150-225", 'batch_size': 128, 'epochs': 3, 'log_diagnostics': True, 'log_diagnostics_every_epoch': True, 'log_diagnostics_deciles': True, } run.run(runargs)
deep-variance-reduction-main
reproduce/reproduce_iterate_distance.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import os import sys import run vr_froms = [1, 21, 41, 61, 81, 1234] try: pindex = int(sys.argv[1]) print(f"problem index {pindex}") except: pindex = 0 seed = 0 method = methods[pindex] runargs = { 'vr_from_epoch': vr_froms[pindex], 'method': 'recompute_svrg', 'problem': 'imagenet', 'architecture': 'resnet50', 'momentum': 0.9, 'lr': 0.1, 'decay': 0.0001, 'lr_reduction': "every30", 'batch_size': 256, 'epochs': 100, 'save_model': True, 'full_checkpointing': True, 'log_interval': 80, } run.run(runargs)
deep-variance-reduction-main
reproduce/reproduce_finetuning.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from __future__ import print_function import pickle import glob import os import pdb import math import numpy as np import scipy.stats import itertools import random from random import shuffle from collections import OrderedDict #import normalization.positive_normalization #from normalization import positive_normalization #from normalization import * #from label_lines import * import matplotlib as mpl mpl.use('agg') import matplotlib.pyplot as plt plt.ioff() #http://matplotlib.org/faq/usage_faq.html (interactive mode) #mpl.rcParams['mathtext.fontset'] = 'cm' mpl.rcParams['font.family'] = 'serif' mpl.rcParams['font.size'] = '6' linewidth = '0.3' mpl.rcParams['lines.linewidth'] = linewidth mpl.rcParams['axes.linewidth'] = linewidth mpl.rcParams['xtick.major.width'] = linewidth mpl.rcParams['ytick.major.width'] = linewidth label_fontsize = 3 linestyles = ['-', '--', '-.', '-', '-', '--', '-.', '-'] #colors = ["#659BC9", "#551a8b", "#e41a1c", "#377eb8"] #101, 155, 201 is #659BC9. 236, 163, 57 is #ECA339 colors = ["#000000", "#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#ffff33", "#a65628", "#f781bf"] loss_type="test_errors" ylim=(20, 70) #100-77 xlim=(0, 90) figname = "finetuning_densenet.pdf" runs = [ '/checkpoint/adefazio/opt/vr_densenet_resume/20191022_070415j60d2vd4/job-0/log/run123/current_runinfo.pkl', '/checkpoint/adefazio/opt/vr_densenet_sweep2/20191018_113200kfr3mjvy/job-4/log/run123/current_runinfo.pkl', #'/checkpoint/adefazio/opt/vr_densenet_sweep2/20191018_113200kfr3mjvy/job-5/log/run123/current_runinfo.pkl', ] traces = [] plt.cla() scalefactor = 0.85 fig = plt.figure(figsize=(scalefactor*3.3,scalefactor*2)) ax = fig.add_subplot(111) ax.set_prop_cycle("color", colors) plt.xlabel('Epoch') plt.ylabel("Test error (%)") legend_position = 'upper right' idx = 0 for fname in runs: print("(ALL) processing run ", fname) with open(fname, 'rb') as fdata: rd = pickle.load(fdata) args = rd['args'] losses = rd[loss_type] losses = losses[:xlim[1]] losses = 100.0 - np.array(losses) legend = f"from {args.vr_from_epoch:2d} ({min(losses):1.2f}%)" if len(losses) > 0: x = list(range(1,len(losses)+1)) y = losses.copy() ax.plot(x, y, label=legend, linestyle=linestyles[idx % len(linestyles)]) idx += 1 #pdb.set_trace() print("Finalizing plot") if ylim is not None: plt.ylim(ylim) if xlim is not None: plt.xlim(xlim) baseline = 23.22 ax.axhline(baseline, color="r", label=f"Baseline ({baseline:1.2f}%)") ax.grid(False) ax.xaxis.set_tick_params(direction='in') ax.yaxis.set_tick_params(direction='in', right=True) #labelLines(plt.gca().get_lines(), align=False, fontsize=label_fontsize, xvals=xvals) ax.legend(fontsize=5, handlelength=2, loc=legend_position) #bbox_to_anchor=(1, 0.5) fig.savefig(figname, bbox_inches='tight', pad_inches=0) print("saved", figname)
deep-variance-reduction-main
reproduce/plot_finetuning.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import os import sys import run archs = ['default', 'resnet-small', 'densenet-40-36', 'resnet110'] try: pindex = int(sys.argv[1]) print(f"problem index {pindex}") except: pindex = 0 arch = archs[pindex] runargs = { 'problem': 'cifar10', 'architecture': arch, 'method': "svrg", 'logfname': 'reproduce-variance-ratios-{}'.format(arch), 'momentum': 0.9, 'decay': 0.0001, 'lr': 0.1, 'lr_reduction': "150-225", 'batch_size': 128, 'epochs': 250, 'log_diagnostics': True, } run.run(runargs)
deep-variance-reduction-main
reproduce/reproduce_ratio_plots.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from __future__ import print_function import pickle import glob import os import re import numpy as np import itertools import random from random import shuffle import pdb import matplotlib as mpl mpl.use('agg') import matplotlib.pyplot as plt plt.ioff() #http://matplotlib.org/faq/usage_faq.html (interactive mode) import numpy as np import itertools from random import shuffle from matplotlib.ticker import FuncFormatter from label_lines import * run_dir = "runs" plot_dir = "plots" if not os.path.exists(plot_dir): os.makedirs(plot_dir) #mpl.rcParams['mathtext.fontset'] = 'cm' mpl.rcParams['font.family'] = 'serif' mpl.rcParams['font.size'] = '6' linewidth = '0.3' mpl.rcParams['lines.linewidth'] = linewidth mpl.rcParams['axes.linewidth'] = linewidth mpl.rcParams['xtick.major.width'] = linewidth mpl.rcParams['ytick.major.width'] = linewidth label_fontsize = 6 linestyles = itertools.cycle(('-', '--', '-.', ':')) colors = ["#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#ffff33", "#a65628", "#f781bf"] def plot_dist(plot_name, data_files, labels, xvals): ylabel = "Iterate distance" epochs = [] plt.cla() fig = plt.figure(figsize=(3.2,2)) ax = fig.add_subplot(111) ax.set_prop_cycle("color", colors) for fname, label in zip(data_files, labels): print("(ALL) processing file ", fname) with open(fname, 'rb') as fdata: rd = pickle.load(fdata) #pdb.set_trace() if 'batch_indices' in rd: print("Has batch indices") # Calculate x axis for plotting batch_indices = np.array(rd["batch_indices"]) nk = len(batch_indices) if max(batch_indices) == min(batch_indices): eval_points = np.array(range(nk))/nk else: eval_points = batch_indices/max(batch_indices) epochs.append(rd["epoch"]) #pdb.set_trace() var_points = rd["iterate_distances"] ax.plot(eval_points, var_points, label=label) # Only compared data from the same epoch if len(set(epochs)) > 1: print("More than one epoch encountered: {}".format(epochs)) print("Finalizing plot") plt.xlabel('Progress within epoch') plt.ylabel(ylabel) plt.xlim([0.0, 1.0]) # Format x axis as percentage def myfunc(x, pos=0): return '%1.0f%%'%(100*x) ax.xaxis.set_major_formatter(FuncFormatter(myfunc)) ax.grid(False) ax.xaxis.set_tick_params(direction='in') ax.yaxis.set_tick_params(direction='in', right="on") #pdb.set_trace() labelLines(plt.gca().get_lines(), align=False, fontsize=label_fontsize, xvals=xvals) figname = "{}/{}.pdf".format(plot_dir, plot_name) fig.savefig(figname, bbox_inches='tight', pad_inches=0) print("saved", figname) ################## plot_dist(plot_name = "variance_dist", data_files = [ "data/variance-dist/default-small_tlockvariance_epoch3.pkl", "data/variance-dist/densenet_tlockvariance_epoch3.pkl"], labels = ["LeNet", "DenseNet"], xvals= [0.65, 0.8])
deep-variance-reduction-main
reproduce/plot_iterate_distance.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import os import sys import run methods = ["sgd", "recompute_svrg", "scsg"] try: pindex = int(sys.argv[1]) seed = int(sys.argv[2]) print(f"problem index {pindex}") except: pindex = 0 seed = 0 method = methods[pindex] runargs = { 'method': method, 'seed': seed, 'problem': 'imagenet', 'architecture': 'resnet18', 'momentum': 0.9, 'lr': 0.1, 'decay': 0.0001, 'lr_reduction': "every30", 'batch_size': 256, 'epochs': 90, 'save_model': True, 'full_checkpointing': True, 'log_interval': 80, } run.run(runargs)
deep-variance-reduction-main
reproduce/reproduce_test_error_imagenet.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import os import sys import run methods = ["sgd", "recompute_svrg", "scsg"] try: pindex = int(sys.argv[1]) seed = int(sys.argv[2]) print(f"problem index {pindex}") except: pindex = 0 seed = 0 method = methods[pindex] runargs = { 'problem': 'cifar10', 'architecture': 'resnet110', #'resnet110', 'method': method, 'seed': seed, 'momentum': 0.9, 'decay': 0.0001, 'lr': 0.05, 'lr_reduction': "150-225", 'batch_size': 128, 'epochs': 250, 'log_diagnostics': False, 'save_model': True, } run.run(runargs)
deep-variance-reduction-main
reproduce/reproduce_test_error_resnet110.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from __future__ import print_function import pickle import glob import os import pdb import matplotlib as mpl mpl.use('agg') import matplotlib.pyplot as plt plt.ioff() #http://matplotlib.org/faq/usage_faq.html (interactive mode) import numpy as np import itertools import scipy import scipy.stats from random import shuffle from label_lines import * #mpl.rcParams['mathtext.fontset'] = 'cm' mpl.rcParams['font.family'] = 'serif' mpl.rcParams['font.size'] = '6' linewidth = '0.3' mpl.rcParams['lines.linewidth'] = linewidth mpl.rcParams['axes.linewidth'] = linewidth mpl.rcParams['xtick.major.width'] = linewidth mpl.rcParams['ytick.major.width'] = linewidth label_fontsize = 6 linestyles = ['-', '--', '-.', '-', '-', '--', '-.', '-'] colors = ["#000000", "#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#ffff33", "#a65628", "#f781bf"] def plot_averaged(plot_name, plot_entries, xvals, yrange=None): run_dir = "runs" plot_dir = "plots" loss_key = "test_errors" ylabel = "Test error (%)" # Positions of the labels in x axis range, i.e. epoch number #xvals if not os.path.exists(plot_dir): os.makedirs(plot_dir) max_seen = 0 plt.cla() fig = plt.figure(figsize=(3.3,2)) ax = fig.add_subplot(111) ax.set_prop_cycle("color", colors) #ax.set_prop_cycle("linestyle", linestyles) line_idx = 0 for plot_entry in plot_entries: fname_grob = plot_entry["fname"] data_files = glob.glob(fname_grob) if len(data_files) == 0: raise Exception("No files found matching path: {}".format(fname_grob)) errors_lists = [] for fname in data_files: print("(ALL) processing run ", fname) with open(fname, 'rb') as fdata: rd = pickle.load(fdata) values = rd[loss_key] # convert to errors errors = [100.0 - val for val in values] #pdb.set_trace() #print("losses: {}".format(losses)) print("Final test error {} for {}".format(errors[-1], plot_entry["label"])) # Remove outlier runs if errors[-1] < 20.0: errors_lists.append(errors.copy()) max_test_loss = max(errors) if max_test_loss > max_seen: max_seen = max_test_loss max_epoch = len(errors) ## Aggregate and plots n = len(errors_lists) errors_avg = [0.0 for i in range(len(errors_lists[0]))] errors_low = [0.0 for i in range(len(errors_lists[0]))] errors_hi = [0.0 for i in range(len(errors_lists[0]))] #pdb.set_trace() # Apply a smoothing filter box_pts = 10 box = np.ones(box_pts)/box_pts for i in range(len(errors_lists)): errors_lists[i] = np.convolve(errors_lists[i], box, mode='valid') # Change from a list of runs to a list of epochs errors = np.array(errors_lists).T.tolist() for i in range(len(errors)): sem = scipy.stats.sem(errors[i]) errors_avg[i] = np.mean(errors[i]) errors_low[i] = errors_avg[i] - sem errors_hi[i] = errors_avg[i] + sem x = range(len(errors_avg)) ax.plot( x, errors_avg, label=plot_entry["label"], linestyle=linestyles[line_idx]) #linestyle=next(linestyles) ax.fill_between(x, errors_low, errors_hi, alpha=0.3) line_idx += 1 print("Average final test error {} for {}".format(errors_avg[-1], plot_entry["label"])) print("Finalizing plot") plt.xlabel('Epoch') plt.ylabel(ylabel) plt.xlim([0, max_epoch-box_pts]) pdb.set_trace() if yrange is not None: plt.ylim(yrange) else: plt.ylim([0, max_seen]) #box = ax.get_position() #ax.set_position([box.x0, box.y0, box.width * 0.6, box.height]) ax.grid(False) ax.xaxis.set_tick_params(direction='in') ax.yaxis.set_tick_params(direction='in', right="on") labelLines(plt.gca().get_lines(), align=False, fontsize=label_fontsize, xvals=xvals) #ax.legend(fontsize=5, handlelength=8, loc='center left', bbox_to_anchor=(1, 0.5)) figname = "{}/{}.pdf".format(plot_dir, plot_name) fig.savefig(figname, bbox_inches='tight', pad_inches=0) print("saved", figname) plot_averaged( plot_name="test_resnet110_V2", plot_entries = [ {"fname": "runs/cifar10/*resnet110*scsg*.pkl", "label": "SCSG"}, {"fname": "runs/cifar10/*resnet110*sgd*.pkl", "label": "SGD"}, {"fname": "runs/cifar10/*resnet110*svrg*.pkl", "label": "SVRG"}, ], xvals=[175, 210, 100], yrange=[6.0, 40.0])
deep-variance-reduction-main
reproduce/plot_test_error_with_bars.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import os import sys import run methods = ["sgd", "recompute_svrg", "scsg"] try: pindex = int(sys.argv[1]) seed = int(sys.argv[2]) print(f"problem index {pindex}") except: pindex = 0 seed = 0 method = methods[pindex] runargs = { 'problem': 'cifar10', 'architecture': 'default', 'method': method, 'seed': seed, 'momentum': 0.9, 'decay': 0.0001, 'lr': 0.1, 'lr_reduction': "150-225", 'batch_size': 128, 'epochs': 250, 'log_diagnostics': False, 'save_model': True, } run.run(runargs)
deep-variance-reduction-main
reproduce/reproduce_test_error_lenet.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from __future__ import print_function import pickle import glob import os import re import matplotlib.ticker as plticker import numpy as np import itertools import random from random import shuffle import pdb import matplotlib as mpl mpl.use('agg') import matplotlib.pyplot as plt import matplotlib.ticker as plticker plt.ioff() #http://matplotlib.org/faq/usage_faq.html (interactive mode) import numpy as np import itertools from random import shuffle from label_lines import * run_dir = "runs" plot_dir = "plots" if not os.path.exists(plot_dir): os.makedirs(plot_dir) #mpl.rcParams['mathtext.fontset'] = 'cm' mpl.rcParams['font.family'] = 'serif' mpl.rcParams['font.size'] = '6' linewidth = '0.3' mpl.rcParams['lines.linewidth'] = linewidth mpl.rcParams['axes.linewidth'] = linewidth mpl.rcParams['xtick.major.width'] = linewidth mpl.rcParams['ytick.major.width'] = linewidth label_fontsize = 6 linestyles = itertools.cycle(('-', '--', '-.', ':')) colors = ["black", "#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#ffff33", "#a65628", "#f781bf"] def plot_variance_ratios(plot_name, data_files_grob, xvals): ylabel = "SVR Variance / SGD Variance" keys_to_show = ['2%', '11%', '33%', '100%'] # Position of in-plot labels along the x axis epochs = [] ratios = [] vr_variances = [] gradient_variances = [] trace_data = {} data_files = glob.glob(data_files_grob) def atoi(text): return int(text) if text.isdigit() else text def natural_keys(text): return [ atoi(c) for c in re.split('(\d+)', text) ] data_files.sort(key=natural_keys) #pdb.set_trace() for fname in data_files: print("(ALL) processing file ", fname) with open(fname, 'rb') as fdata: rd = pickle.load(fdata) #pdb.set_trace() if 'batch_indices' in rd: print("Has batch indices") # Calculate x axis for plotting batch_indices = np.array(rd["batch_indices"]) nk = len(batch_indices) if max(batch_indices) == min(batch_indices): eval_points = np.array(range(nk))/nk else: eval_points = batch_indices/max(batch_indices) epochs.append(rd["epoch"]) #pdb.set_trace() ratio_points = (np.array(rd["vr_step_variances"])/np.array(rd["gradient_variances"])).tolist() for i, ep in enumerate(eval_points): ep_name = "{0:.0f}%".format(100*ep) if ep_name not in trace_data.keys(): trace_data[ep_name] = [ratio_points[i]] else: trace_data[ep_name].append(ratio_points[i]) plt.cla() fig = plt.figure(figsize=(3.2,2)) ax = fig.add_subplot(111) ax.set_prop_cycle("color", colors) #pdb.set_trace() for ep_name, data in trace_data.items(): if ep_name in keys_to_show: ax.plot(epochs, data, ".", label=ep_name) #, linestyle=next(linestyles)) if ep_name == "100%": print("100p epochs:", epochs) print("ratios: ", data) print("Finalizing plot") plt.xlabel('Epoch') plt.ylabel(ylabel) ax.set_yscale("log", basey=2) ax.set_yticks([2**(-i) for i in range(0, 11)]) plt.ylim([1e-3, 3]) plt.xlim([0.0, 240]) # Horizontal line at 1 #plt.axhline(y=1.0, color="#000000", linestyle='--') #plt.axhline(y=2.0, color="#000000", linestyle='--') ax.axhspan(1, 2, alpha=0.3, facecolor='red', edgecolor=None) # Step size reduction indicators plt.axvline(x=150.0, color="brown", linestyle='--') plt.axvline(x=220.0, color="brown", linestyle='--') #loc = plticker.LogLocator(base=2.0) #ax.yaxis.set_major_locator(loc) #plt.tick_params(axis='y', which='minor') ax.grid(False) ax.xaxis.set_tick_params(direction='in') ax.yaxis.set_tick_params(direction='in', right="on") labelLines(plt.gca().get_lines(), align=False, fontsize=label_fontsize, xvals=xvals) figname = "{}/{}.pdf".format(plot_dir, plot_name) fig.savefig(figname, bbox_inches='tight', pad_inches=0) print("saved", figname) ################## xvals are low percentages to high plot_variance_ratios(plot_name = "variance_ratios_densenet", data_files_grob = "data/variance1/var-*.pkl", xvals = [200, 200, 200, 200]) plot_variance_ratios(plot_name = "variance_ratios_lenet", data_files_grob = "data/variance-lenet/*.pkl", xvals = [210, 200, 190, 210]) plot_variance_ratios(plot_name = "variance_ratios_small-resnet", data_files_grob = "data/variance-small-resnet/*.pkl", xvals = [180, 180, 180, 210]) plot_variance_ratios(plot_name = "variance_ratios_resnet110", data_files_grob = "data/variance-resnet110/*.pkl", xvals = [180, 180, 180, 210]) # Soft versions if True: plot_variance_ratios(plot_name = "soft_variance_ratios_densenet", data_files_grob = "data/variance-soft/*densenet*.pkl", xvals = [200, 200, 200, 200]) plot_variance_ratios(plot_name = "soft_variance_ratios_lenet", data_files_grob = "data/variance-soft/*default*.pkl", xvals = [210, 200, 190, 210]) plot_variance_ratios(plot_name = "soft_variance_ratios_small-resnet", data_files_grob = "data/variance-soft/*resnet*.pkl", xvals = [180, 180, 180, 210])
deep-variance-reduction-main
reproduce/plot_variance_ratio.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import os import sys import run transform_locking = [True, False] try: pindex = int(sys.argv[1]) print(f"problem index {pindex}") except: pindex = 0 locking = transform_locking[pindex] runargs = { 'transform_locking': locking, 'problem': 'cifar10', 'architecture': 'default', 'method': "svrg", 'logfname': 'reproduce-transform-locking-{}'.format(locking), 'momentum': 0.9, 'decay': 0.0001, 'lr': 0.1, 'lr_reduction': "150-225", 'batch_size': 128, 'epochs': 3, 'log_diagnostics': True, 'log_diagnostics_every_epoch': True, 'log_diagnostics_deciles': True, } run.run(runargs)
deep-variance-reduction-main
reproduce/reproduce_locking_plot.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from __future__ import print_function import pickle import glob import os import pdb import matplotlib as mpl mpl.use('agg') import matplotlib.pyplot as plt plt.ioff() #http://matplotlib.org/faq/usage_faq.html (interactive mode) import numpy as np import itertools from random import shuffle from label_lines import * mpl.rcParams['font.family'] = 'serif' mpl.rcParams['font.size'] = '6' linewidth = '0.3' mpl.rcParams['lines.linewidth'] = linewidth mpl.rcParams['axes.linewidth'] = linewidth mpl.rcParams['xtick.major.width'] = linewidth mpl.rcParams['ytick.major.width'] = linewidth label_fontsize = 6 linestyles = ['-', '--', '-.', '-', '-', '--', '-.', '-'] colors = ["#000000", "#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#ffff33", "#a65628", "#f781bf"] def plot_averaged(plot_name, plot_entries, xvals, yrange=None): run_dir = "runs" plot_dir = "plots" #plot_name = "test_error1" loss_key = "test_errors" ylabel = "Test error (%)" # Positions of the labels in x axis range, i.e. epoch number #xvals if not os.path.exists(plot_dir): os.makedirs(plot_dir) max_seen = 0 plt.cla() fig = plt.figure(figsize=(3.3,2)) ax = fig.add_subplot(111) ax.set_prop_cycle("color", colors) #ax.set_prop_cycle("linestyle", linestyles) line_idx = 0 for plot_entry in plot_entries: fname_grob = plot_entry["fname"] data_files = glob.glob(fname_grob) if len(data_files) == 0: raise Exception("No files found matching path: {}".format(fname_grob)) errors_lists = [] for fname in data_files: print("(ALL) processing run ", fname) with open(fname, 'rb') as fdata: rd = pickle.load(fdata) values = rd[loss_key] # convert to errors errors = [100.0 - val for val in values] #pdb.set_trace() #print("losses: {}".format(losses)) print("Final test error {} for {}".format(errors[-1], plot_entry["label"])) errors_lists.append(errors.copy()) max_test_loss = max(errors) if max_test_loss > max_seen: max_seen = max_test_loss max_epoch = len(errors) ## Aggregate and plots n = len(errors_lists) errors_avg = [0.0 for i in range(len(errors_lists[0]))] for i in range(n): for j in range(len(errors_avg)): errors_avg[j] += float(errors_lists[i][j]/n) #pdb.set_trace() ax.plot( range(len(errors_avg)), errors_avg, label=plot_entry["label"], linestyle=linestyles[line_idx]) #linestyle=next(linestyles) line_idx += 1 print("Average final test error {} for {}".format(errors_avg[-1], plot_entry["label"])) print("Finalizing plot") plt.xlabel('Epoch') plt.ylabel(ylabel) plt.xlim([0, max_epoch]) if yrange is not None: plt.ylim(yrange) else: plt.ylim([0, max_seen]) #box = ax.get_position() #ax.set_position([box.x0, box.y0, box.width * 0.6, box.height]) ax.grid(False) ax.xaxis.set_tick_params(direction='in') ax.yaxis.set_tick_params(direction='in', right="on") labelLines(plt.gca().get_lines(), align=False, fontsize=label_fontsize, xvals=xvals) #ax.legend(fontsize=5, handlelength=8, loc='center left', bbox_to_anchor=(1, 0.5)) figname = "{}/{}.pdf".format(plot_dir, plot_name) fig.savefig(figname, bbox_inches='tight', pad_inches=0) print("saved", figname) ####################################################### ###################################################### plot_averaged( plot_name="test_resnet110_V2", plot_entries = [ {"fname": "runs/cifar10/*resnet110*scsg*.pkl", "label": "SCSG"}, {"fname": "runs/cifar10/*resnet110*sgd*.pkl", "label": "SGD"}, {"fname": "runs/cifar10/*resnet110*svrg*.pkl", "label": "SVRG"}, ], xvals=[175, 210, 250], yrange=[0, 40.0]) # plot_averaged( # plot_name="test_lenet", # plot_entries = [ # {"fname": "data/runs-large/cifar10-default-scsg-m0_9d0_0001lr0_1sl1e-06epochs300bs128pb_Falseis_10drop_Falsebn_Truereduct_150-225seed_*.pkl", "label": "SCSG"}, # {"fname": "data/runs-large/cifar10-default-sgd-m0_9d0_0001lr0_1sl1e-06epochs300bs128pb_Falseis_10drop_Falsebn_Truereduct_150-225seed_*.pkl", "label": "SGD"}, # {"fname": "data/runs-large/cifar10-default-svrg-lr0_1-m0_9-d0_0001-epochs300bs128drop_Falsebn_Truevr_from_1bn_recal_Truereduct_150-225seed_*.pkl", "label": "SVRG"}, # ], # xvals=[175, 210, 250], # yrange=[20.0, 40.0]) # plot_averaged( # plot_name="test_imagenet", # plot_entries = [ # {"fname": "data/imagenet-vr/*scsg*.pkl", "label": "SCSG"}, # {"fname": "data/imagenet-sgd/*.pkl", "label": "SGD"}, # {"fname": "data/imagenet-vr/*svrg*.pkl", "label": "SVRG"}, # ], # xvals=[22, 45, 10], # yrange=[30.0, 70.0])
deep-variance-reduction-main
reproduce/plot_test_error.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. from __future__ import print_function import pickle import glob import os import re import numpy as np import itertools import random from random import shuffle import pdb import matplotlib as mpl mpl.use('agg') import matplotlib.pyplot as plt plt.ioff() #http://matplotlib.org/faq/usage_faq.html (interactive mode) import numpy as np import itertools from random import shuffle from matplotlib.ticker import FuncFormatter from label_lines import * run_dir = "runs" plot_dir = "plots" if not os.path.exists(plot_dir): os.makedirs(plot_dir) #mpl.rcParams['mathtext.fontset'] = 'cm' mpl.rcParams['font.family'] = 'serif' mpl.rcParams['font.size'] = '6' linewidth = '0.3' mpl.rcParams['lines.linewidth'] = linewidth mpl.rcParams['axes.linewidth'] = linewidth mpl.rcParams['xtick.major.width'] = linewidth mpl.rcParams['ytick.major.width'] = linewidth label_fontsize = 6 linestyles = itertools.cycle(('-', '--', '-.', ':')) colors = ["#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#ffff33", "#a65628", "#f781bf"] def plot_variance_raw(plot_name, data_files, labels): ylabel = "SVRG Variance" xvals = [0.7, 0.7] # Position of in-plot labels along the x axis epochs = [] plt.cla() fig = plt.figure(figsize=(3.2,2)) ax = fig.add_subplot(111) ax.set_prop_cycle("color", colors) for fname, label in zip(data_files, labels): print("(ALL) processing file ", fname) with open(fname, 'rb') as fdata: rd = pickle.load(fdata) #pdb.set_trace() if 'batch_indices' in rd: print("Has batch indices") # Calculate x axis for plotting batch_indices = np.array(rd["batch_indices"]) nk = len(batch_indices) if max(batch_indices) == min(batch_indices): eval_points = np.array(range(nk))/nk else: eval_points = batch_indices/max(batch_indices) epochs.append(rd["epoch"]) #pdb.set_trace() var_points = rd["vr_step_variances"] #pdb.set_trace() ax.plot(eval_points, var_points, label=label) # Only compared data from the same epoch if len(set(epochs)) > 1: print("More than one epoch encountered: {}".format(epochs)) print("Finalizing plot") plt.xlabel('Progress within epoch') plt.ylabel(ylabel) plt.ylim([0, 0.7]) plt.xlim([0.0, 1.0]) # Format x axis as percentage def myfunc(x, pos=0): return '%1.0f%%'%(100*x) ax.xaxis.set_major_formatter(FuncFormatter(myfunc)) ax.grid(False) ax.xaxis.set_tick_params(direction='in') ax.yaxis.set_tick_params(direction='in', right="on") labelLines(plt.gca().get_lines(), align=False, fontsize=label_fontsize, xvals=xvals) figname = "{}/{}.pdf".format(plot_dir, plot_name) fig.savefig(figname, bbox_inches='tight', pad_inches=0) print("saved", figname) ################## plot_variance_raw(plot_name = "variance_transform", data_files = [ "data/variance-locking/default-lenet-shortvariance_epoch3.pkl", "data/variance-locking/default-lenet-short_tlockvariance_epoch3.pkl", ], labels = ["No locking", "Transform locking"])
deep-variance-reduction-main
reproduce/plot_transform_locking.py
# Copyright (c) Meta Platforms, Inc. and affiliates. # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import json import sys class Fab_HDD(): def __init__(self, config="BarraCuda"): ############################### # Carbon per capacity ############################### with open("hdd/hdd_consumer.json", 'r') as f: hdd_config = json.load(f) with open("hdd/hdd_enterprise.json", 'r') as f: hdd_config.update(json.load(f)) assert config in hdd_config.keys() and "HDD configuration not found" self.carbon_per_gb = hdd_config[config] self.carbon = 0 return def get_cpg(self, ): return self.carbon_per_gb def set_capacity(self, capacity): self.capacity = capacity self.carbon = carbon_per_gb return def get_carbon(self, ): return self.carbon
ACT-main
hdd_model.py
# Copyright (c) Meta Platforms, Inc. and affiliates. # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import json import sys class Fab_Logic(): def __init__(self, process_node=14, gpa="97", carbon_intensity="loc_taiwan", debug=False, fab_yield=0.875): self.debug = debug ############################### # Energy per unit area ############################### with open("logic/epa.json", 'r') as f: epa_config = json.load(f) ############################### # Raw materials per unit area ############################### with open("logic/materials.json", 'r') as f: materials_config = json.load(f) ############################### # Gasses per unit area ############################### if gpa == "95": with open("logic/gpa_95.json", 'r') as f: gpa_config = json.load(f) elif gpa == "99": with open("logic/gpa_99.json", 'r') as f: gpa_config = json.load(f) elif gpa == "97": with open("logic/gpa_95.json", 'r') as f: gpa_95_config = json.load(f) with open("logic/gpa_99.json", 'r') as f: gpa_99_config = json.load(f) gpa_config = {} for c in gpa_95_config.keys(): gas = (gpa_95_config[c] + gpa_99_config[c]) / 2. gpa_config[c] = gas else: print("Error: Unsupported GPA value for FAB logic") sys.exit() ############################### # Carbon intensity of fab ############################### if "loc" in carbon_intensity: with open("carbon_intensity/location.json", 'r') as f: loc_configs = json.load(f) loc = carbon_intensity.replace("loc_", "") assert loc in loc_configs.keys() fab_ci = loc_configs[loc] elif "src" in carbon_intensity: with open("carbon_intensity/source.json", 'r') as f: src_configs = json.load(f) src = carbon_intensity.replace("src_", "") assert src in src_configs.keys() fab_ci = src_configs[src] else: print("Error: Carbon intensity must either be loc | src dependent") sys.exit() ############################### # Aggregating model ############################### process_node = str(process_node) + "nm" assert process_node in epa_config.keys() assert process_node in gpa_config.keys() assert process_node in materials_config.keys() carbon_energy = fab_ci * epa_config[process_node] carbon_gas = gpa_config[process_node] carbon_materials = materials_config[process_node] self.carbon_per_area = (carbon_energy + carbon_gas + carbon_materials) self.carbon_per_area = self.carbon_per_area / fab_yield if self.debug: print("[Fab logic] Carbon/area from energy consumed" , carbon_energy) print("[Fab logic] Carbon/area from gasses" , carbon_gas) print("[Fab logic] Carbon/area from materials" , carbon_materials) print("[Fab logic] Carbon/area aggregate" , self.carbon_per_area) self.carbon = 0 return def get_cpa(self,): return self.carbon_per_area def set_area(self, area): self.area = area self.carbon = self.area * self.carbon_per_area def get_carbon(self, ): return self.carbon
ACT-main
logic_model.py
# Copyright (c) Meta Platforms, Inc. and affiliates. # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import json import sys from dram_model import Fab_DRAM from ssd_model import Fab_SSD from logic_model import Fab_Logic def main(): Fab_DRAM(config="ddr4_10nm") Fab_SSD(config="nand_10nm") Fab_Logic(gpa="95", carbon_intensity = "src_coal", debug=True, process_node=10) # Fab_Logic(gpa="97", carbon_intensity = "loc_taiwan", debug=True, # process_node=14) if __name__=="__main__": main()
ACT-main
model.py
# Copyright (c) Meta Platforms, Inc. and affiliates. # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import json import sys class Fab_DRAM(): def __init__(self, config = "ddr4_10nm", fab_yield=0.875): ############################### # Carbon per capacity ############################### with open("dram/dram_hynix.json", 'r') as f: dram_config = json.load(f) assert config in dram_config.keys() and "DRAM configuration not found" self.fab_yield = fab_yield self.carbon_per_gb = dram_config[config] / self.fab_yield self.carbon = 0 def get_cpg(self, ): return self.carbon_per_gb def set_capacity(self, capacity): self.capacity = capacity self.carbon = self.carbon_per_gb * self.capacity return def get_carbon(self, ): return self.carbon
ACT-main
dram_model.py
# Copyright (c) Meta Platforms, Inc. and affiliates. # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import json import sys class Fab_SSD(): def __init__(self, config="nand_10nm", fab_yield=0.875): ############################### # Carbon per capacity ############################### with open("ssd/ssd_hynix.json", 'r') as f: ssd_config = json.load(f) with open("ssd/ssd_seagate.json", 'r') as f: ssd_config.update(json.load(f)) with open("ssd/ssd_western.json", 'r') as f: ssd_config.update(json.load(f)) assert config in ssd_config.keys() and "SSD configuration not found" self.fab_yield = fab_yield self.carbon_per_gb = ssd_config[config] / self.fab_yield self.carbon = 0 return def get_cpg(self, ): return self.carbon_per_gb def set_capacity(self, capacity): self.capacity = capacity self.carbon = self.carbon_per_gb * self.capacity return def get_carbon(self, ): return self.carbon
ACT-main
ssd_model.py
# Copyright (c) Meta Platforms, Inc. and affiliates. # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import json import sys from dram_model import Fab_DRAM from hdd_model import Fab_HDD from ssd_model import Fab_SSD from logic_model import Fab_Logic debug = False ############################## # Original Dell 740 LCA ############################## #https://corporate.delltechnologies.com/content/dam/digitalassets/active/en/unauth/data-sheets/products/servers/lca_poweredge_r740.pdf ############################## # Main Dell R740 integrated circuits ############################## dellr740_large_ssd = 3840 # GB (3.84 TB x 8 SSD's) dellr740_ssd = 400 # GB (400GB x 1 SSD) dellr740_ssd_dram = 68 # GB (64 + 4GB ECC) dellr740_dram = 36 # GB (32 + 4 ECC GB x 12) ic_yield = 0.875 cpu_area = 6.98 #cm^2 ############################## # Estimated process technology node to mimic fairphone LCA process node ############################## CPU_Logic = Fab_Logic(gpa = "95", carbon_intensity = "src_coal", process_node = 28, fab_yield=ic_yield) SSD_main = Fab_SSD(config = "nand_30nm", fab_yield = ic_yield) SSD_secondary = Fab_SSD(config = "nand_30nm", fab_yield = ic_yield) DRAM_SSD_main = Fab_DRAM(config = "ddr3_50nm", fab_yield = ic_yield) DRAM_SSD_secondary = Fab_DRAM(config = "ddr3_50nm", fab_yield = ic_yield) DRAM = Fab_DRAM(config = "ddr3_50nm", fab_yield = ic_yield) ############################## # Computing carbon footprint of IC's ############################## CPU_Logic.set_area(cpu_area) DRAM.set_capacity(dellr740_dram) DRAM_SSD_main.set_capacity(dellr740_ssd_dram) SSD_main.set_capacity(dellr740_large_ssd) DRAM_SSD_secondary.set_capacity(dellr740_ssd_dram) SSD_secondary.set_capacity(dellr740_ssd) ################################## # Computing the packaging footprint ################################## # number of packages ssd_main_nr = 12 + 1 ssd_secondary_nr = 12 + 1 dram_nr = 18 + 1 cpu_nr = 2 packaging_intensity = 150 # gram CO2 SSD_main_packaging = packaging_intensity * ssd_main_nr SSD_secondary_packaging = packaging_intensity * ssd_secondary_nr DRAM_packging = packaging_intensity * dram_nr CPU_packaging = packaging_intensity * cpu_nr total_packaging = SSD_main_packaging + \ SSD_secondary_packaging + \ DRAM_packging + \ CPU_packaging total_packaging = total_packaging / 1000. ################################## # Compute end-to-end carbon footprints ################################## SSD_main_count = 8 # There are 8x3.84TB SSD's SSD_main_co2 = (SSD_main.get_carbon() + \ DRAM_SSD_main.get_carbon() + \ SSD_main_packaging) / 1000. SSD_main_co2 = SSD_main_co2 * SSD_main_count SSD_secondary_count = 1 # There are 1x400GB SSD's SSD_secondary_co2 = (SSD_secondary.get_carbon() + \ DRAM_SSD_secondary.get_carbon() + \ SSD_secondary_packaging) / 1000. SSD_secondary_co2 = SSD_secondary_co2 * SSD_secondary_count DRAM_count = 12 # There are 12 x (32GB+4GB ECC DRAM modules) DRAM_co2 = (DRAM.get_carbon() + DRAM_packging) / 1000. * DRAM_count CPU_count = 2 CPU_co2 = (CPU_Logic.get_carbon() + CPU_packaging) * CPU_count / 1000. if debug: print("ACT SSD main", SSD_main_co2, "kg CO2") print("ACT SSD secondary", SSD_secondary_co2, "kg CO2") print("ACT DRAM", DRAM_co2, "kg CO2") print("ACT CPU", CPU_co2, "kg CO2") print("ACT Packaging", total_packaging, "kg CO2") print("--------------------------------") print("ACT SSD main", SSD_main_co2, "kg CO2 vs. LCA 3373 kg CO2") print("ACT SSD secondary", SSD_secondary_co2, "kg CO2 vs. LCA 64.1 kg CO2") print("ACT DRAM", DRAM_co2, "kg CO2 vs. LCA 533 kg CO2") print("ACT CPU", CPU_co2, "kg CO2 vs. LCA 47 kg CO2")
ACT-main
exps/dellr740/dellr740.py
# Copyright (c) Meta Platforms, Inc. and affiliates. # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import json import sys from dram_model import Fab_DRAM from hdd_model import Fab_HDD from ssd_model import Fab_SSD from logic_model import Fab_Logic debug = False # Main Fairphone integrated circuits fairphone3_ICs = ["IC analog switch", "LED Flash", "LED Flash", "CMOS image sensor", "Light sensor", "Light sensor", "LED Full Color", "Image sensor", "I.C WLAN", "I.C WLAN", "Audio power amplifier", "IC analog switch", "IC power amplifier", "IC PMU", "IC PMU", "IC PMU", "Sensor", "NFC Microcontroller", "IC transceiver", "IC audio power", ] # Main Fairphone integrated circuits' areas in mm^2 fairphone3_IC_areas = [0.85, 1.2, 1.2, 35, 0.89, 0.08, 0.25, 18, 11.6, 1.44, 12.96, 1.61, 6.3, 26.88, 0.77, 11.36, 7, 8.69, 11, 9.6] fairphone_cpu_area = 46.4 #mm^2 fairphone_ram = 4 # GB fairphone_storage = 64 # GB ic_yield = 0.875 ################################## # Estimated process technology node to mimic fairphone LCA process node # This initializes ACT with an older technology node. ################################## # IC Logic node IC_Logic = Fab_Logic(gpa = "95", carbon_intensity = "src_coal", process_node = 28, fab_yield=ic_yield) # CPU Application processor node CPU_Logic = Fab_Logic(gpa = "95", carbon_intensity = "src_coal", process_node = 28, fab_yield=ic_yield) # DRAM Logic node DRAM = Fab_DRAM(config = "ddr3_50nm", fab_yield=ic_yield) # SSD Logic node SSD = Fab_SSD(config = "nand_30nm", fab_yield=ic_yield) ################################## # Computing the IC footprint ################################## IC_Logic.set_area(sum(fairphone3_IC_areas)/100.) CPU_Logic.set_area(fairphone_cpu_area/100.) DRAM.set_capacity(fairphone_ram) SSD.set_capacity(fairphone_storage) ################################## # Computing the packaging footprint ################################## #Number of packages nr = len(fairphone3_ICs) + 1 + 1 + 1 # Fairphone ICs + CPU + DRAM + SSD packaging_intensity = 150 # gram CO2 PackagingFootprint = nr * packaging_intensity if debug: print("ACT IC", IC_Logic.get_carbon(), "g CO2") print("ACT CPU", CPU_Logic.get_carbon(), "g CO2") print("ACT DRAM", DRAM.get_carbon(), "g CO2") print("ACT SSD", SSD.get_carbon(), "g CO2") print("ACT Packaging", PackagingFootprint, "g CO2") print("--------------------------------") ram_flash = (DRAM.get_carbon() + SSD.get_carbon() + packaging_intensity * 2) / 1000. fairphone_ram_flash = 11 print("ACT RAM + Flash", ram_flash, "kg CO2 vs. LCA", fairphone_ram_flash, "kg CO2") cpu = (CPU_Logic.get_carbon() + packaging_intensity) / 1000. fairphone_cpu = 1.07 print("ACT CPU", cpu, "kg CO2 vs. LCA", fairphone_cpu, "kg CO2") ics = (IC_Logic.get_carbon() + packaging_intensity * len(fairphone3_ICs)) / 1000. fairphone_ics = 5.3 print("ACT ICs", ics, "kg CO2 vs. LCA", fairphone_ics, "kg CO2")
ACT-main
exps/fairphone3/fairphone3.py
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # import time import faiss import numpy as np from PIL import Image from PIL import ImageFile from scipy.sparse import csr_matrix, find import torch import torch.utils.data as data import torchvision.transforms as transforms ImageFile.LOAD_TRUNCATED_IMAGES = True __all__ = ['PIC', 'Kmeans', 'cluster_assign', 'arrange_clustering'] def pil_loader(path): """Loads an image. Args: path (string): path to image file Returns: Image """ with open(path, 'rb') as f: img = Image.open(f) return img.convert('RGB') class ReassignedDataset(data.Dataset): """A dataset where the new images labels are given in argument. Args: image_indexes (list): list of data indexes pseudolabels (list): list of labels for each data dataset (list): list of tuples with paths to images transform (callable, optional): a function/transform that takes in an PIL image and returns a transformed version """ def __init__(self, image_indexes, pseudolabels, dataset, transform=None): self.imgs = self.make_dataset(image_indexes, pseudolabels, dataset) self.transform = transform def make_dataset(self, image_indexes, pseudolabels, dataset): label_to_idx = {label: idx for idx, label in enumerate(set(pseudolabels))} images = [] for j, idx in enumerate(image_indexes): path = dataset[idx][0] pseudolabel = label_to_idx[pseudolabels[j]] images.append((path, pseudolabel)) return images def __getitem__(self, index): """ Args: index (int): index of data Returns: tuple: (image, pseudolabel) where pseudolabel is the cluster of index datapoint """ path, pseudolabel = self.imgs[index] img = pil_loader(path) if self.transform is not None: img = self.transform(img) return img, pseudolabel def __len__(self): return len(self.imgs) def preprocess_features(npdata, pca=256): """Preprocess an array of features. Args: npdata (np.array N * ndim): features to preprocess pca (int): dim of output Returns: np.array of dim N * pca: data PCA-reduced, whitened and L2-normalized """ _, ndim = npdata.shape npdata = npdata.astype('float32') # Apply PCA-whitening with Faiss mat = faiss.PCAMatrix (ndim, pca, eigen_power=-0.5) mat.train(npdata) assert mat.is_trained npdata = mat.apply_py(npdata) # L2 normalization row_sums = np.linalg.norm(npdata, axis=1) npdata = npdata / row_sums[:, np.newaxis] return npdata def make_graph(xb, nnn): """Builds a graph of nearest neighbors. Args: xb (np.array): data nnn (int): number of nearest neighbors Returns: list: for each data the list of ids to its nnn nearest neighbors list: for each data the list of distances to its nnn NN """ N, dim = xb.shape # we need only a StandardGpuResources per GPU res = faiss.StandardGpuResources() # L2 flat_config = faiss.GpuIndexFlatConfig() flat_config.device = int(torch.cuda.device_count()) - 1 index = faiss.GpuIndexFlatL2(res, dim, flat_config) index.add(xb) D, I = index.search(xb, nnn + 1) return I, D def cluster_assign(images_lists, dataset): """Creates a dataset from clustering, with clusters as labels. Args: images_lists (list of list): for each cluster, the list of image indexes belonging to this cluster dataset (list): initial dataset Returns: ReassignedDataset(torch.utils.data.Dataset): a dataset with clusters as labels """ assert images_lists is not None pseudolabels = [] image_indexes = [] for cluster, images in enumerate(images_lists): image_indexes.extend(images) pseudolabels.extend([cluster] * len(images)) normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) t = transforms.Compose([transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize]) return ReassignedDataset(image_indexes, pseudolabels, dataset, t) def run_kmeans(x, nmb_clusters, verbose=False): """Runs kmeans on 1 GPU. Args: x: data nmb_clusters (int): number of clusters Returns: list: ids of data in each cluster """ n_data, d = x.shape # faiss implementation of k-means clus = faiss.Clustering(d, nmb_clusters) # Change faiss seed at each k-means so that the randomly picked # initialization centroids do not correspond to the same feature ids # from an epoch to another. clus.seed = np.random.randint(1234) clus.niter = 20 clus.max_points_per_centroid = 10000000 res = faiss.StandardGpuResources() flat_config = faiss.GpuIndexFlatConfig() flat_config.useFloat16 = False flat_config.device = 0 index = faiss.GpuIndexFlatL2(res, d, flat_config) # perform the training clus.train(x, index) _, I = index.search(x, 1) losses = faiss.vector_to_array(clus.obj) if verbose: print('k-means loss evolution: {0}'.format(losses)) return [int(n[0]) for n in I], losses[-1] def arrange_clustering(images_lists): pseudolabels = [] image_indexes = [] for cluster, images in enumerate(images_lists): image_indexes.extend(images) pseudolabels.extend([cluster] * len(images)) indexes = np.argsort(image_indexes) return np.asarray(pseudolabels)[indexes] class Kmeans(object): def __init__(self, k): self.k = k def cluster(self, data, verbose=False): """Performs k-means clustering. Args: x_data (np.array N * dim): data to cluster """ end = time.time() # PCA-reducing, whitening and L2-normalization xb = preprocess_features(data) # cluster the data I, loss = run_kmeans(xb, self.k, verbose) self.images_lists = [[] for i in range(self.k)] for i in range(len(data)): self.images_lists[I[i]].append(i) if verbose: print('k-means time: {0:.0f} s'.format(time.time() - end)) return loss def make_adjacencyW(I, D, sigma): """Create adjacency matrix with a Gaussian kernel. Args: I (numpy array): for each vertex the ids to its nnn linked vertices + first column of identity. D (numpy array): for each data the l2 distances to its nnn linked vertices + first column of zeros. sigma (float): Bandwidth of the Gaussian kernel. Returns: csr_matrix: affinity matrix of the graph. """ V, k = I.shape k = k - 1 indices = np.reshape(np.delete(I, 0, 1), (1, -1)) indptr = np.multiply(k, np.arange(V + 1)) def exp_ker(d): return np.exp(-d / sigma**2) exp_ker = np.vectorize(exp_ker) res_D = exp_ker(D) data = np.reshape(np.delete(res_D, 0, 1), (1, -1)) adj_matrix = csr_matrix((data[0], indices[0], indptr), shape=(V, V)) return adj_matrix def run_pic(I, D, sigma, alpha): """Run PIC algorithm""" a = make_adjacencyW(I, D, sigma) graph = a + a.transpose() cgraph = graph nim = graph.shape[0] W = graph t0 = time.time() v0 = np.ones(nim) / nim # power iterations v = v0.astype('float32') t0 = time.time() dt = 0 for i in range(200): vnext = np.zeros(nim, dtype='float32') vnext = vnext + W.transpose().dot(v) vnext = alpha * vnext + (1 - alpha) / nim # L1 normalize vnext /= vnext.sum() v = vnext if i == 200 - 1: clust = find_maxima_cluster(W, v) return [int(i) for i in clust] def find_maxima_cluster(W, v): n, m = W.shape assert (n == m) assign = np.zeros(n) # for each node pointers = list(range(n)) for i in range(n): best_vi = 0 l0 = W.indptr[i] l1 = W.indptr[i + 1] for l in range(l0, l1): j = W.indices[l] vi = W.data[l] * (v[j] - v[i]) if vi > best_vi: best_vi = vi pointers[i] = j n_clus = 0 cluster_ids = -1 * np.ones(n) for i in range(n): if pointers[i] == i: cluster_ids[i] = n_clus n_clus = n_clus + 1 for i in range(n): # go from pointers to pointers starting from i until reached a local optim current_node = i while pointers[current_node] != current_node: current_node = pointers[current_node] assign[i] = cluster_ids[current_node] assert (assign[i] >= 0) return assign class PIC(object): """Class to perform Power Iteration Clustering on a graph of nearest neighbors. Args: args: for consistency with k-means init sigma (float): bandwidth of the Gaussian kernel (default 0.2) nnn (int): number of nearest neighbors (default 5) alpha (float): parameter in PIC (default 0.001) distribute_singletons (bool): If True, reassign each singleton to the cluster of its closest non singleton nearest neighbors (up to nnn nearest neighbors). Attributes: images_lists (list of list): for each cluster, the list of image indexes belonging to this cluster """ def __init__(self, args=None, sigma=0.2, nnn=5, alpha=0.001, distribute_singletons=True): self.sigma = sigma self.alpha = alpha self.nnn = nnn self.distribute_singletons = distribute_singletons def cluster(self, data, verbose=False): end = time.time() # preprocess the data xb = preprocess_features(data) # construct nnn graph I, D = make_graph(xb, self.nnn) # run PIC clust = run_pic(I, D, self.sigma, self.alpha) images_lists = {} for h in set(clust): images_lists[h] = [] for data, c in enumerate(clust): images_lists[c].append(data) # allocate singletons to clusters of their closest NN not singleton if self.distribute_singletons: clust_NN = {} for i in images_lists: # if singleton if len(images_lists[i]) == 1: s = images_lists[i][0] # for NN for n in I[s, 1:]: # if NN is not a singleton if not len(images_lists[clust[n]]) == 1: clust_NN[s] = n break for s in clust_NN: del images_lists[clust[s]] clust[s] = clust[clust_NN[s]] images_lists[clust[s]].append(s) self.images_lists = [] for c in images_lists: self.images_lists.append(images_lists[c]) if verbose: print('pic time: {0:.0f} s'.format(time.time() - end)) return 0
deepcluster-main
clustering.py
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # import os import pickle import numpy as np import torch from torch.utils.data.sampler import Sampler import models def load_model(path): """Loads model and return it without DataParallel table.""" if os.path.isfile(path): print("=> loading checkpoint '{}'".format(path)) checkpoint = torch.load(path) # size of the top layer N = checkpoint['state_dict']['top_layer.bias'].size() # build skeleton of the model sob = 'sobel.0.weight' in checkpoint['state_dict'].keys() model = models.__dict__[checkpoint['arch']](sobel=sob, out=int(N[0])) # deal with a dataparallel table def rename_key(key): if not 'module' in key: return key return ''.join(key.split('.module')) checkpoint['state_dict'] = {rename_key(key): val for key, val in checkpoint['state_dict'].items()} # load weights model.load_state_dict(checkpoint['state_dict']) print("Loaded") else: model = None print("=> no checkpoint found at '{}'".format(path)) return model class UnifLabelSampler(Sampler): """Samples elements uniformely accross pseudolabels. Args: N (int): size of returned iterator. images_lists: dict of key (target), value (list of data with this target) """ def __init__(self, N, images_lists): self.N = N self.images_lists = images_lists self.indexes = self.generate_indexes_epoch() def generate_indexes_epoch(self): nmb_non_empty_clusters = 0 for i in range(len(self.images_lists)): if len(self.images_lists[i]) != 0: nmb_non_empty_clusters += 1 size_per_pseudolabel = int(self.N / nmb_non_empty_clusters) + 1 res = np.array([]) for i in range(len(self.images_lists)): # skip empty clusters if len(self.images_lists[i]) == 0: continue indexes = np.random.choice( self.images_lists[i], size_per_pseudolabel, replace=(len(self.images_lists[i]) <= size_per_pseudolabel) ) res = np.concatenate((res, indexes)) np.random.shuffle(res) res = list(res.astype('int')) if len(res) >= self.N: return res[:self.N] res += res[: (self.N - len(res))] return res def __iter__(self): return iter(self.indexes) def __len__(self): return len(self.indexes) class AverageMeter(object): """Computes and stores the average and current value""" def __init__(self): self.reset() def reset(self): self.val = 0 self.avg = 0 self.sum = 0 self.count = 0 def update(self, val, n=1): self.val = val self.sum += val * n self.count += n self.avg = self.sum / self.count def learning_rate_decay(optimizer, t, lr_0): for param_group in optimizer.param_groups: lr = lr_0 / np.sqrt(1 + lr_0 * param_group['weight_decay'] * t) param_group['lr'] = lr class Logger(object): """ Class to update every epoch to keep trace of the results Methods: - log() log and save """ def __init__(self, path): self.path = path self.data = [] def log(self, train_point): self.data.append(train_point) with open(os.path.join(self.path), 'wb') as fp: pickle.dump(self.data, fp, -1)
deepcluster-main
util.py
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # import argparse from collections import OrderedDict import os import pickle import subprocess import sys import numpy as np from PIL import Image import torch import torchvision from torch.autograd import Variable from util import load_model class ImageHelper: def __init__(self, S, L, transforms): self.S = S self.L = L self.transforms = transforms def load_and_prepare_image(self, fname, roi=None): # Read image, get aspect ratio, and resize such as the largest side equals S im = Image.open(fname) im_size_hw = np.array((im.size[1], im.size[0])) if self.S == -1: ratio = 1.0 elif self.S == -2: if np.max(im_size_hw) > 124: ratio = 1024.0/np.max(im_size_hw) else: ratio = -1 else: ratio = float(self.S)/np.max(im_size_hw) new_size = tuple(np.round(im_size_hw * ratio).astype(np.int32)) im_resized = self.transforms(im.resize((new_size[1], new_size[0]), Image.BILINEAR)) # If there is a roi, adapt the roi to the new size and crop. Do not rescale # the image once again if roi is not None: # ROI format is (xmin,ymin,xmax,ymax) roi = np.round(roi * ratio).astype(np.int32) im_resized = im_resized[:, roi[1]:roi[3], roi[0]:roi[2]] return im_resized def get_rmac_region_coordinates(self, H, W, L): # Almost verbatim from Tolias et al Matlab implementation. # Could be heavily pythonized, but really not worth it... # Desired overlap of neighboring regions ovr = 0.4 # Possible regions for the long dimension steps = np.array((2, 3, 4, 5, 6, 7), dtype=np.float32) w = np.minimum(H, W) b = (np.maximum(H, W) - w) / (steps - 1) # steps(idx) regions for long dimension. The +1 comes from Matlab # 1-indexing... idx = np.argmin(np.abs(((w**2 - w * b) / w**2) - ovr)) + 1 # Region overplus per dimension Wd = 0 Hd = 0 if H < W: Wd = idx elif H > W: Hd = idx regions_xywh = [] for l in range(1, L+1): wl = np.floor(2 * w / (l + 1)) wl2 = np.floor(wl / 2 - 1) # Center coordinates if l + Wd - 1 > 0: b = (W - wl) / (l + Wd - 1) else: b = 0 cenW = np.floor(wl2 + b * np.arange(l - 1 + Wd + 1)) - wl2 # Center coordinates if l + Hd - 1 > 0: b = (H - wl) / (l + Hd - 1) else: b = 0 cenH = np.floor(wl2 + b * np.arange(l - 1 + Hd + 1)) - wl2 for i_ in cenH: for j_ in cenW: regions_xywh.append([j_, i_, wl, wl]) # Round the regions. Careful with the borders! for i in range(len(regions_xywh)): for j in range(4): regions_xywh[i][j] = int(round(regions_xywh[i][j])) if regions_xywh[i][0] + regions_xywh[i][2] > W: regions_xywh[i][0] -= ((regions_xywh[i][0] + regions_xywh[i][2]) - W) if regions_xywh[i][1] + regions_xywh[i][3] > H: regions_xywh[i][1] -= ((regions_xywh[i][1] + regions_xywh[i][3]) - H) return np.array(regions_xywh) class PCA(object): ''' Fits and applies PCA whitening ''' def __init__(self, n_components): self.n_components = n_components def fit(self, X): mean = X.mean(axis=0) X -= mean self.mean = Variable(torch.from_numpy(mean).view(1, -1)) Xcov = np.dot(X.T, X) d, V = np.linalg.eigh(Xcov) eps = d.max() * 1e-5 n_0 = (d < eps).sum() if n_0 > 0: print("%d / %d singular values are 0" % (n_0, d.size)) d[d < eps] = eps totenergy = d.sum() idx = np.argsort(d)[::-1][:self.n_components] d = d[idx] V = V[:, idx] print("keeping %.2f %% of the energy" % (d.sum() / totenergy * 100.0)) D = np.diag(1. / np.sqrt(d)) self.DVt = Variable(torch.from_numpy(np.dot(D, V.T))) def to_cuda(self): self.mean = self.mean.cuda() self.DVt = self.DVt.cuda() def apply(self, X): X = X - self.mean num = torch.mm(self.DVt, X.transpose(0, 1)).transpose(0, 1) # L2 normalize on output return num class Dataset: def __init__(self, path, eval_binary_path): self.path = path self.eval_binary_path = eval_binary_path # Some images from the Paris dataset are corrupted. Standard practice is # to ignore them self.blacklisted = set(["paris_louvre_000136", "paris_louvre_000146", "paris_moulinrouge_000422", "paris_museedorsay_001059", "paris_notredame_000188", "paris_pantheon_000284", "paris_pantheon_000960", "paris_pantheon_000974", "paris_pompidou_000195", "paris_pompidou_000196", "paris_pompidou_000201", "paris_pompidou_000467", "paris_pompidou_000640", "paris_sacrecoeur_000299", "paris_sacrecoeur_000330", "paris_sacrecoeur_000353", "paris_triomphe_000662", "paris_triomphe_000833", "paris_triomphe_000863", "paris_triomphe_000867"]) self.load() def load(self): # Load the dataset GT self.lab_root = '{0}/lab/'.format(self.path) self.img_root = '{0}/jpg/'.format(self.path) lab_filenames = np.sort(os.listdir(self.lab_root)) # Get the filenames without the extension self.img_filenames = [e[:-4] for e in np.sort(os.listdir(self.img_root)) if e[:-4] not in self.blacklisted] # Parse the label files. Some challenges as filenames do not correspond # exactly to query names. Go through all the labels to: # i) map names to filenames and vice versa # ii) get the relevant regions of interest of the queries, # iii) get the indexes of the dataset images that are queries # iv) get the relevants / non-relevants list self.relevants = {} self.junk = {} self.non_relevants = {} self.filename_to_name = {} self.name_to_filename = OrderedDict() self.q_roi = {} for e in lab_filenames: if e.endswith('_query.txt'): q_name = e[:-len('_query.txt')] q_data = open("{0}/{1}".format(self.lab_root, e)).readline().split(" ") q_filename = q_data[0][5:] if q_data[0].startswith('oxc1_') else q_data[0] self.filename_to_name[q_filename] = q_name self.name_to_filename[q_name] = q_filename good = set([e.strip() for e in open("{0}/{1}_ok.txt".format(self.lab_root, q_name))]) good = good.union(set([e.strip() for e in open("{0}/{1}_good.txt".format(self.lab_root, q_name))])) junk = set([e.strip() for e in open("{0}/{1}_junk.txt".format(self.lab_root, q_name))]) good_plus_junk = good.union(junk) self.relevants[q_name] = [i for i in range(len(self.img_filenames)) if self.img_filenames[i] in good] self.junk[q_name] = [i for i in range(len(self.img_filenames)) if self.img_filenames[i] in junk] self.non_relevants[q_name] = [i for i in range(len(self.img_filenames)) if self.img_filenames[i] not in good_plus_junk] self.q_roi[q_name] = np.array([float(q) for q in q_data[1:]], dtype=np.float32) #np.array(map(float, q_data[1:]), dtype=np.float32) self.q_names = self.name_to_filename.keys() self.q_index = np.array([self.img_filenames.index(self.name_to_filename[qn]) for qn in self.q_names]) self.N_images = len(self.img_filenames) self.N_queries = len(self.q_index) def score(self, sim, temp_dir, eval_bin): if not os.path.exists(temp_dir): os.makedirs(temp_dir) idx = np.argsort(sim, axis=1)[:, ::-1] maps = [self.score_rnk_partial(i, idx[i], temp_dir, eval_bin) for i in range(len(self.q_names))] for i in range(len(self.q_names)): print("{0}: {1:.2f}".format(self.q_names[i], 100 * maps[i])) print(20 * "-") print("Mean: {0:.2f}".format(100 * np.mean(maps))) def score_rnk_partial(self, i, idx, temp_dir, eval_bin): rnk = np.array(self.img_filenames)[idx] with open("{0}/{1}.rnk".format(temp_dir, self.q_names[i]), 'w') as f: f.write("\n".join(rnk)+"\n") cmd = "{0} {1}{2} {3}/{4}.rnk".format(eval_bin, self.lab_root, self.q_names[i], temp_dir, self.q_names[i]) p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) map_ = float(p.stdout.readlines()[0]) p.wait() return map_ def get_filename(self, i): return os.path.normpath("{0}/{1}.jpg".format(self.img_root, self.img_filenames[i])) def get_query_filename(self, i): return os.path.normpath("{0}/{1}.jpg".format(self.img_root, self.img_filenames[self.q_index[i]])) def get_query_roi(self, i): return self.q_roi[self.q_names[i]] def ensure_directory_exists(fname): dirname = fname[:fname.rfind('/')] if not os.path.exists(dirname): os.makedirs(dirname) def normalize_L2(a, dim): norms = torch.sqrt(torch.sum(a**2, dim=dim, keepdim=True)) return a / norms def rmac(features, rmac_levels, pca=None): nim, nc, xd, yd = features.size() rmac_regions = image_helper.get_rmac_region_coordinates(xd, yd, rmac_levels) rmac_regions = rmac_regions.astype(np.int) nr = len(rmac_regions) rmac_descriptors = [] for x0, y0, w, h in rmac_regions: desc = features[:, :, y0:y0 + h, x0:x0 + w] desc = torch.max(desc, 2, keepdim=True)[0] desc = torch.max(desc, 3, keepdim=True)[0] # insert an additional dimension for the cat to work rmac_descriptors.append(desc.view(-1, 1, nc)) rmac_descriptors = torch.cat(rmac_descriptors, 1) rmac_descriptors = normalize_L2(rmac_descriptors, 2) if pca is None: return rmac_descriptors # PCA + whitening npca = pca.n_components rmac_descriptors = pca.apply(rmac_descriptors.view(nr * nim, nc)) rmac_descriptors = normalize_L2(rmac_descriptors, 1) rmac_descriptors = rmac_descriptors.view(nim, nr, npca) # Sum aggregation and L2-normalization rmac_descriptors = torch.sum(rmac_descriptors, 1) rmac_descriptors = normalize_L2(rmac_descriptors, 1) return rmac_descriptors if __name__ == '__main__': parser = argparse.ArgumentParser(description='Evaluate Oxford / Paris') parser.add_argument('--S', type=int, default=1024, help='Resize larger side of image to S pixels (e.g. 800)') parser.add_argument('--L', type=int, default=3, help='Use L spatial levels (e.g. 3)') parser.add_argument('--n_pca', type=int, default=512, help='output dimension of PCA') parser.add_argument('--model', type=str, default='pretrained', help='Model from which RMAC is computed') parser.add_argument('--dataset', type=str, required=True, help='path to dataset') parser.add_argument('--dataset_name', type=str, default='Oxford', choices=['Oxford', 'Paris'], help='Dataset name') parser.add_argument('--stage', type=str, default='extract_train', choices=['extract_train', 'train_pca', 'db_features', 'q_features', 'eval'], help='what action to perform ') parser.add_argument('--eval_binary', type=str, required=True, help='Path to the compute_ap binary to evaluate Oxford / Paris') parser.add_argument('--temp_dir', type=str, default='', help='Path to a temporary directory to store features and scores') parser.add_argument('--multires', dest='multires', action='store_true', help='Enable multiresolution features') parser.add_argument('--aqe', type=int, required=False, help='Average query expansion with k neighbors') parser.add_argument('--dbe', type=int, required=False, help='Database expansion with k neighbors') parser.set_defaults(multires=False) args = parser.parse_args() # Load the dataset and the image helper print "Prepare the dataset from ", args.dataset dataset = Dataset(args.dataset, args.eval_binary) ensure_directory_exists(args.temp_dir + '/') if args.stage in ('extract_train', 'db_features', 'q_features'): if args.model == 'pretrained': print("loading supervised pretrained VGG-16") net = torchvision.models.vgg16_bn(pretrained=True) else: net = load_model(args.model) transforms_comp = [] features_layers = list(net.features.children())[:-1] net.features = torch.nn.Sequential(*features_layers) transforms_comp.extend([ torchvision.transforms.ToTensor(), torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) transforms = torchvision.transforms.Compose(transforms_comp) print("moving to GPU") net.cuda() net.eval() print(" done") print("initialize image helper") image_helper = ImageHelper(args.S, args.L, transforms) if args.stage == 'extract_train': print("extract regions for training") # extract at a single scale S = args.S image_helper.S = S N_dataset = dataset.N_images def process_image(i): print(i), sys.stdout.flush() fname_out = "{0}/{1}_S{2}_L{3}_regions/{4}.npy".format(args.temp_dir, args.dataset_name, S, args.L, i) ensure_directory_exists(fname_out) I = image_helper.load_and_prepare_image(dataset.get_filename(i), roi=None) v = torch.autograd.Variable(I.unsqueeze(0)) vc = v.cuda() if hasattr(net, 'sobel') and net.sobel is not None: vc = net.sobel(vc) activation_map = net.features(vc).cpu() rmac_descriptors = rmac(activation_map, args.L) np.save(fname_out, rmac_descriptors.data.numpy()) map(process_image, range(dataset.N_images)) elif args.stage == 'train_pca': # load training vectors train_x = [] for i in range(10000): fname_in = "{0}/{1}_S{2}_L{3}_regions/{4}.npy".format(args.temp_dir, args.dataset_name, args.S, args.L, i) if not os.path.exists(fname_in): break x = np.load(fname_in) train_x.append(x) print("loaded %d train vectors" % len(train_x)) train_x = np.vstack([x.reshape(-1, x.shape[-1]) for x in train_x]) print(" size", train_x.shape) pca = PCA(args.n_pca) pca.fit(train_x) pcaname = '%s/%s_S%d_PCA.pickle' % (args.temp_dir, args.dataset_name, args.S) print("writing", pcaname) pickle.dump(pca, open(pcaname, 'w'), -1) elif args.stage == 'db_features' or args.stage == 'q_features': # for tests on Paris, use Oxford PCA, and vice-versa pcaname = '%s/%s_S%d_PCA.pickle' % ( args.temp_dir, 'Paris' if args.dataset_name == 'Oxford' else 'Oxford', args.S) print("loading PCA from", pcaname) pca = pickle.load(open(pcaname, 'r')) print("Compute features") # extract at a single scale S = args.S image_helper.S = S N_dataset = dataset.N_images def process_image(fname_in, roi, fname_out): softmax = torch.nn.Softmax().cuda() I = image_helper.load_and_prepare_image(fname_in, roi=roi) v = torch.autograd.Variable(I.unsqueeze(0)) vc = v.cuda() if hasattr(net, 'sobel') and net.sobel is not None: vc = net.sobel(vc) activation_map = net.features(vc).cpu() descriptors = rmac(activation_map, args.L, pca=pca) np.save(fname_out, descriptors.data.numpy()) if args.stage == 'db_features': for i in range(dataset.N_images): fname_in = dataset.get_filename(i) fname_out = "{0}/{1}_S{2}_L{3}_db/{4}.npy".format(args.temp_dir, args.dataset_name, S, args.L, i) ensure_directory_exists(fname_out) print(i), sys.stdout.flush() process_image(fname_in, None, fname_out) elif args.stage == 'q_features': for i in range(dataset.N_queries): fname_in = dataset.get_query_filename(i) roi = dataset.get_query_roi(i) fname_out = "{0}/{1}_S{2}_L{3}_q/{4}.npy".format(args.temp_dir, args.dataset_name, S, args.L, i) ensure_directory_exists(fname_out) print(i), sys.stdout.flush() process_image(fname_in, roi, fname_out) elif args.stage == 'eval': S = args.S print("load query features") features_queries = [] for i in range(dataset.N_queries): fname = "{0}/{1}_S{2}_L{3}_q/{4}.npy".format(args.temp_dir, args.dataset_name, S, args.L, i) features_queries.append(np.load(fname)) features_queries = np.vstack(features_queries) print(" size", features_queries.shape) print("load database features") features_dataset = [] for i in range(dataset.N_images): fname = "{0}/{1}_S{2}_L{3}_db/{4}.npy".format(args.temp_dir, args.dataset_name, S, args.L, i) features_dataset.append(np.load(fname)) features_dataset = np.vstack(features_dataset) print(" size", features_dataset.shape) # Compute similarity sim = features_queries.dot(features_dataset.T) # Score dataset.score(sim, args.temp_dir, args.eval_binary)
deepcluster-main
eval_retrieval.py
deepcluster-main
__init__.py
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # #!/usr/bin/env python # -*- coding: utf-8 -*- import argparse import os import math import time import glob from collections import defaultdict import numpy as np import torch import torch.nn as nn import torch.optim import torch.utils.data import torchvision import torchvision.transforms as transforms import torch.backends.cudnn as cudnn from sklearn import metrics from PIL import Image from PIL import ImageFile ImageFile.LOAD_TRUNCATED_IMAGES = True from util import AverageMeter, load_model from eval_linear import accuracy parser = argparse.ArgumentParser() parser.add_argument('--vocdir', type=str, required=False, default='', help='pascal voc 2007 dataset') parser.add_argument('--split', type=str, required=False, default='train', choices=['train', 'trainval'], help='training split') parser.add_argument('--model', type=str, required=False, default='', help='evaluate this model') parser.add_argument('--nit', type=int, default=80000, help='Number of training iterations') parser.add_argument('--fc6_8', type=int, default=1, help='If true, train only the final classifier') parser.add_argument('--train_batchnorm', type=int, default=0, help='If true, train batch-norm layer parameters') parser.add_argument('--eval_random_crops', type=int, default=1, help='If true, eval on 10 random crops, otherwise eval on 10 fixed crops') parser.add_argument('--stepsize', type=int, default=5000, help='Decay step') parser.add_argument('--lr', type=float, required=False, default=0.003, help='learning rate') parser.add_argument('--wd', type=float, required=False, default=1e-6, help='weight decay') parser.add_argument('--min_scale', type=float, required=False, default=0.1, help='scale') parser.add_argument('--max_scale', type=float, required=False, default=0.5, help='scale') parser.add_argument('--seed', type=int, default=31, help='random seed') def main(): args = parser.parse_args() print(args) # fix random seeds torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) np.random.seed(args.seed) # create model and move it to gpu model = load_model(args.model) model.top_layer = nn.Linear(model.top_layer.weight.size(1), 20) model.cuda() cudnn.benchmark = True # what partition of the data to use if args.split == 'train': args.test = 'val' elif args.split == 'trainval': args.test = 'test' # data loader normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) dataset = VOC2007_dataset(args.vocdir, split=args.split, transform=transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.RandomResizedCrop(224, scale=(args.min_scale, args.max_scale), ratio=(1, 1)), transforms.ToTensor(), normalize, ])) loader = torch.utils.data.DataLoader(dataset, batch_size=16, shuffle=False, num_workers=24, pin_memory=True) print('PASCAL VOC 2007 ' + args.split + ' dataset loaded') # re initialize classifier for y, m in enumerate(model.classifier.modules()): if isinstance(m, nn.Linear): m.weight.data.normal_(0, 0.01) m.bias.data.fill_(0.1) model.top_layer.bias.data.fill_(0.1) if args.fc6_8: # freeze some layers for param in model.features.parameters(): param.requires_grad = False # unfreeze batchnorm scaling if args.train_batchnorm: for layer in model.modules(): if isinstance(layer, torch.nn.BatchNorm2d): for param in layer.parameters(): param.requires_grad = True # set optimizer optimizer = torch.optim.SGD( filter(lambda x: x.requires_grad, model.parameters()), lr=args.lr, momentum=0.9, weight_decay=args.wd, ) criterion = nn.BCEWithLogitsLoss(reduction='none') print('Start training') it = 0 losses = AverageMeter() while it < args.nit: it = train( loader, model, optimizer, criterion, args.fc6_8, losses, it=it, total_iterations=args.nit, stepsize=args.stepsize, ) print('Evaluation') if args.eval_random_crops: transform_eval = [ transforms.RandomHorizontalFlip(), transforms.RandomResizedCrop(224, scale=(args.min_scale, args.max_scale), ratio=(1, 1)), transforms.ToTensor(), normalize, ] else: transform_eval = [ transforms.Resize(256), transforms.TenCrop(224), transforms.Lambda(lambda crops: torch.stack([normalize(transforms.ToTensor()(crop)) for crop in crops])) ] print('Train set') train_dataset = VOC2007_dataset(args.vocdir, split=args.split, transform=transforms.Compose(transform_eval)) train_loader = torch.utils.data.DataLoader( train_dataset, batch_size=1, shuffle=False, num_workers=24, pin_memory=True, ) evaluate(train_loader, model, args.eval_random_crops) print('Test set') test_dataset = VOC2007_dataset(args.vocdir, split=args.test, transform=transforms.Compose(transform_eval)) test_loader = torch.utils.data.DataLoader( test_dataset, batch_size=1, shuffle=False, num_workers=24, pin_memory=True, ) evaluate(test_loader, model, args.eval_random_crops) def evaluate(loader, model, eval_random_crops): model.eval() gts = [] scr = [] for crop in range(9 * eval_random_crops + 1): for i, (input, target) in enumerate(loader): # move input to gpu and optionally reshape it if len(input.size()) == 5: bs, ncrops, c, h, w = input.size() input = input.view(-1, c, h, w) input = input.cuda(non_blocking=True) # forward pass without grad computation with torch.no_grad(): output = model(input) if crop < 1 : scr.append(torch.sum(output, 0, keepdim=True).cpu().numpy()) gts.append(target) else: scr[i] += output.cpu().numpy() gts = np.concatenate(gts, axis=0).T scr = np.concatenate(scr, axis=0).T aps = [] for i in range(20): # Subtract eps from score to make AP work for tied scores ap = metrics.average_precision_score(gts[i][gts[i]<=1], scr[i][gts[i]<=1]-1e-5*gts[i][gts[i]<=1]) aps.append( ap ) print(np.mean(aps), ' ', ' '.join(['%0.2f'%a for a in aps])) def train(loader, model, optimizer, criterion, fc6_8, losses, it=0, total_iterations=None, stepsize=None, verbose=True): # to log batch_time = AverageMeter() data_time = AverageMeter() top1 = AverageMeter() end = time.time() current_iteration = it # use dropout for the MLP model.train() # in the batch norms always use global statistics model.features.eval() for (input, target) in loader: # measure data loading time data_time.update(time.time() - end) # adjust learning rate if current_iteration != 0 and current_iteration % stepsize == 0: for param_group in optimizer.param_groups: param_group['lr'] = param_group['lr'] * 0.5 print('iter {0} learning rate is {1}'.format(current_iteration, param_group['lr'])) # move input to gpu input = input.cuda(non_blocking=True) # forward pass with or without grad computation output = model(input) target = target.float().cuda() mask = (target == 255) loss = torch.sum(criterion(output, target).masked_fill_(mask, 0)) / target.size(0) # backward optimizer.zero_grad() loss.backward() # clip gradients torch.nn.utils.clip_grad_norm_(model.parameters(), 10) # and weights update optimizer.step() # measure accuracy and record loss losses.update(loss.item(), input.size(0)) # measure elapsed time batch_time.update(time.time() - end) end = time.time() if verbose is True and current_iteration % 25 == 0: print('Iteration[{0}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format( current_iteration, batch_time=batch_time, data_time=data_time, loss=losses)) current_iteration = current_iteration + 1 if total_iterations is not None and current_iteration == total_iterations: break return current_iteration class VOC2007_dataset(torch.utils.data.Dataset): def __init__(self, voc_dir, split='train', transform=None): # Find the image sets image_set_dir = os.path.join(voc_dir, 'ImageSets', 'Main') image_sets = glob.glob(os.path.join(image_set_dir, '*_' + split + '.txt')) assert len(image_sets) == 20 # Read the labels self.n_labels = len(image_sets) images = defaultdict(lambda:-np.ones(self.n_labels, dtype=np.uint8)) for k, s in enumerate(sorted(image_sets)): for l in open(s, 'r'): name, lbl = l.strip().split() lbl = int(lbl) # Switch the ignore label and 0 label (in VOC -1: not present, 0: ignore) if lbl < 0: lbl = 0 elif lbl == 0: lbl = 255 images[os.path.join(voc_dir, 'JPEGImages', name + '.jpg')][k] = lbl self.images = [(k, images[k]) for k in images.keys()] np.random.shuffle(self.images) self.transform = transform def __len__(self): return len(self.images) def __getitem__(self, i): img = Image.open(self.images[i][0]) img = img.convert('RGB') if self.transform is not None: img = self.transform(img) return img, self.images[i][1] if __name__ == '__main__': main()
deepcluster-main
eval_voc_classif.py
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # import argparse import os import pickle import time import faiss import numpy as np from sklearn.metrics.cluster import normalized_mutual_info_score import torch import torch.nn as nn import torch.nn.parallel import torch.backends.cudnn as cudnn import torch.optim import torch.utils.data import torchvision.transforms as transforms import torchvision.datasets as datasets import clustering import models from util import AverageMeter, Logger, UnifLabelSampler def parse_args(): parser = argparse.ArgumentParser(description='PyTorch Implementation of DeepCluster') parser.add_argument('data', metavar='DIR', help='path to dataset') parser.add_argument('--arch', '-a', type=str, metavar='ARCH', choices=['alexnet', 'vgg16'], default='alexnet', help='CNN architecture (default: alexnet)') parser.add_argument('--sobel', action='store_true', help='Sobel filtering') parser.add_argument('--clustering', type=str, choices=['Kmeans', 'PIC'], default='Kmeans', help='clustering algorithm (default: Kmeans)') parser.add_argument('--nmb_cluster', '--k', type=int, default=10000, help='number of cluster for k-means (default: 10000)') parser.add_argument('--lr', default=0.05, type=float, help='learning rate (default: 0.05)') parser.add_argument('--wd', default=-5, type=float, help='weight decay pow (default: -5)') parser.add_argument('--reassign', type=float, default=1., help="""how many epochs of training between two consecutive reassignments of clusters (default: 1)""") parser.add_argument('--workers', default=4, type=int, help='number of data loading workers (default: 4)') parser.add_argument('--epochs', type=int, default=200, help='number of total epochs to run (default: 200)') parser.add_argument('--start_epoch', default=0, type=int, help='manual epoch number (useful on restarts) (default: 0)') parser.add_argument('--batch', default=256, type=int, help='mini-batch size (default: 256)') parser.add_argument('--momentum', default=0.9, type=float, help='momentum (default: 0.9)') parser.add_argument('--resume', default='', type=str, metavar='PATH', help='path to checkpoint (default: None)') parser.add_argument('--checkpoints', type=int, default=25000, help='how many iterations between two checkpoints (default: 25000)') parser.add_argument('--seed', type=int, default=31, help='random seed (default: 31)') parser.add_argument('--exp', type=str, default='', help='path to exp folder') parser.add_argument('--verbose', action='store_true', help='chatty') return parser.parse_args() def main(args): # fix random seeds torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) np.random.seed(args.seed) # CNN if args.verbose: print('Architecture: {}'.format(args.arch)) model = models.__dict__[args.arch](sobel=args.sobel) fd = int(model.top_layer.weight.size()[1]) model.top_layer = None model.features = torch.nn.DataParallel(model.features) model.cuda() cudnn.benchmark = True # create optimizer optimizer = torch.optim.SGD( filter(lambda x: x.requires_grad, model.parameters()), lr=args.lr, momentum=args.momentum, weight_decay=10**args.wd, ) # define loss function criterion = nn.CrossEntropyLoss().cuda() # optionally resume from a checkpoint if args.resume: if os.path.isfile(args.resume): print("=> loading checkpoint '{}'".format(args.resume)) checkpoint = torch.load(args.resume) args.start_epoch = checkpoint['epoch'] # remove top_layer parameters from checkpoint for key in checkpoint['state_dict']: if 'top_layer' in key: del checkpoint['state_dict'][key] model.load_state_dict(checkpoint['state_dict']) optimizer.load_state_dict(checkpoint['optimizer']) print("=> loaded checkpoint '{}' (epoch {})" .format(args.resume, checkpoint['epoch'])) else: print("=> no checkpoint found at '{}'".format(args.resume)) # creating checkpoint repo exp_check = os.path.join(args.exp, 'checkpoints') if not os.path.isdir(exp_check): os.makedirs(exp_check) # creating cluster assignments log cluster_log = Logger(os.path.join(args.exp, 'clusters')) # preprocessing of data normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) tra = [transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize] # load the data end = time.time() dataset = datasets.ImageFolder(args.data, transform=transforms.Compose(tra)) if args.verbose: print('Load dataset: {0:.2f} s'.format(time.time() - end)) dataloader = torch.utils.data.DataLoader(dataset, batch_size=args.batch, num_workers=args.workers, pin_memory=True) # clustering algorithm to use deepcluster = clustering.__dict__[args.clustering](args.nmb_cluster) # training convnet with DeepCluster for epoch in range(args.start_epoch, args.epochs): end = time.time() # remove head model.top_layer = None model.classifier = nn.Sequential(*list(model.classifier.children())[:-1]) # get the features for the whole dataset features = compute_features(dataloader, model, len(dataset)) # cluster the features if args.verbose: print('Cluster the features') clustering_loss = deepcluster.cluster(features, verbose=args.verbose) # assign pseudo-labels if args.verbose: print('Assign pseudo labels') train_dataset = clustering.cluster_assign(deepcluster.images_lists, dataset.imgs) # uniformly sample per target sampler = UnifLabelSampler(int(args.reassign * len(train_dataset)), deepcluster.images_lists) train_dataloader = torch.utils.data.DataLoader( train_dataset, batch_size=args.batch, num_workers=args.workers, sampler=sampler, pin_memory=True, ) # set last fully connected layer mlp = list(model.classifier.children()) mlp.append(nn.ReLU(inplace=True).cuda()) model.classifier = nn.Sequential(*mlp) model.top_layer = nn.Linear(fd, len(deepcluster.images_lists)) model.top_layer.weight.data.normal_(0, 0.01) model.top_layer.bias.data.zero_() model.top_layer.cuda() # train network with clusters as pseudo-labels end = time.time() loss = train(train_dataloader, model, criterion, optimizer, epoch) # print log if args.verbose: print('###### Epoch [{0}] ###### \n' 'Time: {1:.3f} s\n' 'Clustering loss: {2:.3f} \n' 'ConvNet loss: {3:.3f}' .format(epoch, time.time() - end, clustering_loss, loss)) try: nmi = normalized_mutual_info_score( clustering.arrange_clustering(deepcluster.images_lists), clustering.arrange_clustering(cluster_log.data[-1]) ) print('NMI against previous assignment: {0:.3f}'.format(nmi)) except IndexError: pass print('####################### \n') # save running checkpoint torch.save({'epoch': epoch + 1, 'arch': args.arch, 'state_dict': model.state_dict(), 'optimizer' : optimizer.state_dict()}, os.path.join(args.exp, 'checkpoint.pth.tar')) # save cluster assignments cluster_log.log(deepcluster.images_lists) def train(loader, model, crit, opt, epoch): """Training of the CNN. Args: loader (torch.utils.data.DataLoader): Data loader model (nn.Module): CNN crit (torch.nn): loss opt (torch.optim.SGD): optimizer for every parameters with True requires_grad in model except top layer epoch (int) """ batch_time = AverageMeter() losses = AverageMeter() data_time = AverageMeter() forward_time = AverageMeter() backward_time = AverageMeter() # switch to train mode model.train() # create an optimizer for the last fc layer optimizer_tl = torch.optim.SGD( model.top_layer.parameters(), lr=args.lr, weight_decay=10**args.wd, ) end = time.time() for i, (input_tensor, target) in enumerate(loader): data_time.update(time.time() - end) # save checkpoint n = len(loader) * epoch + i if n % args.checkpoints == 0: path = os.path.join( args.exp, 'checkpoints', 'checkpoint_' + str(n / args.checkpoints) + '.pth.tar', ) if args.verbose: print('Save checkpoint at: {0}'.format(path)) torch.save({ 'epoch': epoch + 1, 'arch': args.arch, 'state_dict': model.state_dict(), 'optimizer' : opt.state_dict() }, path) target = target.cuda(async=True) input_var = torch.autograd.Variable(input_tensor.cuda()) target_var = torch.autograd.Variable(target) output = model(input_var) loss = crit(output, target_var) # record loss losses.update(loss.data[0], input_tensor.size(0)) # compute gradient and do SGD step opt.zero_grad() optimizer_tl.zero_grad() loss.backward() opt.step() optimizer_tl.step() # measure elapsed time batch_time.update(time.time() - end) end = time.time() if args.verbose and (i % 200) == 0: print('Epoch: [{0}][{1}/{2}]\t' 'Time: {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data: {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss: {loss.val:.4f} ({loss.avg:.4f})' .format(epoch, i, len(loader), batch_time=batch_time, data_time=data_time, loss=losses)) return losses.avg def compute_features(dataloader, model, N): if args.verbose: print('Compute features') batch_time = AverageMeter() end = time.time() model.eval() # discard the label information in the dataloader for i, (input_tensor, _) in enumerate(dataloader): input_var = torch.autograd.Variable(input_tensor.cuda(), volatile=True) aux = model(input_var).data.cpu().numpy() if i == 0: features = np.zeros((N, aux.shape[1]), dtype='float32') aux = aux.astype('float32') if i < len(dataloader) - 1: features[i * args.batch: (i + 1) * args.batch] = aux else: # special treatment for final batch features[i * args.batch:] = aux # measure elapsed time batch_time.update(time.time() - end) end = time.time() if args.verbose and (i % 200) == 0: print('{0} / {1}\t' 'Time: {batch_time.val:.3f} ({batch_time.avg:.3f})' .format(i, len(dataloader), batch_time=batch_time)) return features if __name__ == '__main__': args = parse_args() main(args)
deepcluster-main
main.py
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # import argparse import os import time import numpy as np import torch import torch.nn as nn import torch.backends.cudnn as cudnn import torch.optim import torch.utils.data import torchvision.transforms as transforms import torchvision.datasets as datasets from util import AverageMeter, learning_rate_decay, load_model, Logger parser = argparse.ArgumentParser(description="""Train linear classifier on top of frozen convolutional layers of an AlexNet.""") parser.add_argument('--data', type=str, help='path to dataset') parser.add_argument('--model', type=str, help='path to model') parser.add_argument('--conv', type=int, choices=[1, 2, 3, 4, 5], help='on top of which convolutional layer train logistic regression') parser.add_argument('--tencrops', action='store_true', help='validation accuracy averaged over 10 crops') parser.add_argument('--exp', type=str, default='', help='exp folder') parser.add_argument('--workers', default=4, type=int, help='number of data loading workers (default: 4)') parser.add_argument('--epochs', type=int, default=90, help='number of total epochs to run (default: 90)') parser.add_argument('--batch_size', default=256, type=int, help='mini-batch size (default: 256)') parser.add_argument('--lr', default=0.01, type=float, help='learning rate') parser.add_argument('--momentum', default=0.9, type=float, help='momentum (default: 0.9)') parser.add_argument('--weight_decay', '--wd', default=-4, type=float, help='weight decay pow (default: -4)') parser.add_argument('--seed', type=int, default=31, help='random seed') parser.add_argument('--verbose', action='store_true', help='chatty') def main(): global args args = parser.parse_args() #fix random seeds torch.manual_seed(args.seed) torch.cuda.manual_seed_all(args.seed) np.random.seed(args.seed) best_prec1 = 0 # load model model = load_model(args.model) model.cuda() cudnn.benchmark = True # freeze the features layers for param in model.features.parameters(): param.requires_grad = False # define loss function (criterion) and optimizer criterion = nn.CrossEntropyLoss().cuda() # data loading code traindir = os.path.join(args.data, 'train') valdir = os.path.join(args.data, 'val') normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) if args.tencrops: transformations_val = [ transforms.Resize(256), transforms.TenCrop(224), transforms.Lambda(lambda crops: torch.stack([normalize(transforms.ToTensor()(crop)) for crop in crops])), ] else: transformations_val = [transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize] transformations_train = [transforms.Resize(256), transforms.CenterCrop(256), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize] train_dataset = datasets.ImageFolder( traindir, transform=transforms.Compose(transformations_train) ) val_dataset = datasets.ImageFolder( valdir, transform=transforms.Compose(transformations_val) ) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.workers, pin_memory=True) val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=int(args.batch_size/2), shuffle=False, num_workers=args.workers) # logistic regression reglog = RegLog(args.conv, len(train_dataset.classes)).cuda() optimizer = torch.optim.SGD( filter(lambda x: x.requires_grad, reglog.parameters()), args.lr, momentum=args.momentum, weight_decay=10**args.weight_decay ) # create logs exp_log = os.path.join(args.exp, 'log') if not os.path.isdir(exp_log): os.makedirs(exp_log) loss_log = Logger(os.path.join(exp_log, 'loss_log')) prec1_log = Logger(os.path.join(exp_log, 'prec1')) prec5_log = Logger(os.path.join(exp_log, 'prec5')) for epoch in range(args.epochs): end = time.time() # train for one epoch train(train_loader, model, reglog, criterion, optimizer, epoch) # evaluate on validation set prec1, prec5, loss = validate(val_loader, model, reglog, criterion) loss_log.log(loss) prec1_log.log(prec1) prec5_log.log(prec5) # remember best prec@1 and save checkpoint is_best = prec1 > best_prec1 best_prec1 = max(prec1, best_prec1) if is_best: filename = 'model_best.pth.tar' else: filename = 'checkpoint.pth.tar' torch.save({ 'epoch': epoch + 1, 'arch': 'alexnet', 'state_dict': model.state_dict(), 'prec5': prec5, 'best_prec1': best_prec1, 'optimizer' : optimizer.state_dict(), }, os.path.join(args.exp, filename)) class RegLog(nn.Module): """Creates logistic regression on top of frozen features""" def __init__(self, conv, num_labels): super(RegLog, self).__init__() self.conv = conv if conv==1: self.av_pool = nn.AvgPool2d(6, stride=6, padding=3) s = 9600 elif conv==2: self.av_pool = nn.AvgPool2d(4, stride=4, padding=0) s = 9216 elif conv==3: self.av_pool = nn.AvgPool2d(3, stride=3, padding=1) s = 9600 elif conv==4: self.av_pool = nn.AvgPool2d(3, stride=3, padding=1) s = 9600 elif conv==5: self.av_pool = nn.AvgPool2d(2, stride=2, padding=0) s = 9216 self.linear = nn.Linear(s, num_labels) def forward(self, x): x = self.av_pool(x) x = x.view(x.size(0), x.size(1) * x.size(2) * x.size(3)) return self.linear(x) def forward(x, model, conv): if hasattr(model, 'sobel') and model.sobel is not None: x = model.sobel(x) count = 1 for m in model.features.modules(): if not isinstance(m, nn.Sequential): x = m(x) if isinstance(m, nn.ReLU): if count == conv: return x count = count + 1 return x def accuracy(output, target, topk=(1,)): """Computes the precision@k for the specified values of k""" maxk = max(topk) batch_size = target.size(0) _, pred = output.topk(maxk, 1, True, True) pred = pred.t() correct = pred.eq(target.view(1, -1).expand_as(pred)) res = [] for k in topk: correct_k = correct[:k].view(-1).float().sum(0, keepdim=True) res.append(correct_k.mul_(100.0 / batch_size)) return res def train(train_loader, model, reglog, criterion, optimizer, epoch): batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() top1 = AverageMeter() top5 = AverageMeter() # freeze also batch norm layers model.eval() end = time.time() for i, (input, target) in enumerate(train_loader): # measure data loading time data_time.update(time.time() - end) #adjust learning rate learning_rate_decay(optimizer, len(train_loader) * epoch + i, args.lr) target = target.cuda(async=True) input_var = torch.autograd.Variable(input.cuda()) target_var = torch.autograd.Variable(target) # compute output output = forward(input_var, model, reglog.conv) output = reglog(output) loss = criterion(output, target_var) # measure accuracy and record loss prec1, prec5 = accuracy(output.data, target, topk=(1, 5)) losses.update(loss.data[0], input.size(0)) top1.update(prec1[0], input.size(0)) top5.update(prec5[0], input.size(0)) # compute gradient and do SGD step optimizer.zero_grad() loss.backward() optimizer.step() # measure elapsed time batch_time.update(time.time() - end) end = time.time() if args.verbose and i % 100 == 0: print('Epoch: [{0}][{1}/{2}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Data {data_time.val:.3f} ({data_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t' 'Prec@5 {top5.val:.3f} ({top5.avg:.3f})' .format(epoch, i, len(train_loader), batch_time=batch_time, data_time=data_time, loss=losses, top1=top1, top5=top5)) def validate(val_loader, model, reglog, criterion): batch_time = AverageMeter() losses = AverageMeter() top1 = AverageMeter() top5 = AverageMeter() # switch to evaluate mode model.eval() softmax = nn.Softmax(dim=1).cuda() end = time.time() for i, (input_tensor, target) in enumerate(val_loader): if args.tencrops: bs, ncrops, c, h, w = input_tensor.size() input_tensor = input_tensor.view(-1, c, h, w) target = target.cuda(async=True) input_var = torch.autograd.Variable(input_tensor.cuda(), volatile=True) target_var = torch.autograd.Variable(target, volatile=True) output = reglog(forward(input_var, model, reglog.conv)) if args.tencrops: output_central = output.view(bs, ncrops, -1)[: , ncrops / 2 - 1, :] output = softmax(output) output = torch.squeeze(output.view(bs, ncrops, -1).mean(1)) else: output_central = output prec1, prec5 = accuracy(output.data, target, topk=(1, 5)) top1.update(prec1[0], input_tensor.size(0)) top5.update(prec5[0], input_tensor.size(0)) loss = criterion(output_central, target_var) losses.update(loss.data[0], input_tensor.size(0)) # measure elapsed time batch_time.update(time.time() - end) end = time.time() if args.verbose and i % 100 == 0: print('Validation: [{0}/{1}]\t' 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t' 'Loss {loss.val:.4f} ({loss.avg:.4f})\t' 'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t' 'Prec@5 {top5.val:.3f} ({top5.avg:.3f})' .format(i, len(val_loader), batch_time=batch_time, loss=losses, top1=top1, top5=top5)) return top1.avg, top5.avg, losses.avg if __name__ == '__main__': main()
deepcluster-main
eval_linear.py
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # import argparse import os from scipy.ndimage.filters import gaussian_filter import sys import numpy as np from PIL import Image import torch import torch.nn as nn import torchvision import torchvision.transforms as transforms sys.path.insert(0, '..') from util import load_model parser = argparse.ArgumentParser(description='Gradient ascent visualisation') parser.add_argument('--model', type=str, help='Model') parser.add_argument('--arch', type=str, default='alexnet', choices=['alexnet', 'vgg16'], help='arch') parser.add_argument('--conv', type=int, default=1, help='convolutional layer') parser.add_argument('--exp', type=str, default='', help='path to res') parser.add_argument('--lr', type=float, default=3, help='learning rate (default: 3)') parser.add_argument('--wd', type=float, default=0.00001, help='weight decay (default: 10^-5)') parser.add_argument('--sig', type=float, default=0.3, help='gaussian blur (default: 0.3)') parser.add_argument('--step', type=int, default=5, help='number of iter between gaussian blurs (default: 5)') parser.add_argument('--niter', type=int, default=1000, help='total number of iterations (default: 1000)') parser.add_argument('--idim', type=int, default=224, help='size of input image (default: 224)') CONV = {'alexnet': [96, 256, 384, 384, 256], 'vgg16': [64, 64, 128, 128, 256, 256, 256, 512, 512, 512, 512, 512, 512]} def main(): args = parser.parse_args() # sanity check if args.arch == 'alexnet': assert args.conv < 6 elif args.arch == 'vgg16': assert args.conv < 14 # create repo repo = os.path.join(args.exp, 'conv' + str(args.conv)) if not os.path.isdir(repo): os.makedirs(repo) # build model model = load_model(args.model) model.cuda() for params in model.parameters(): params.requires_grad = False model.eval() def gradient_ascent(f): print f, sys.stdout.flush() fname_out = '{0}/layer{1}-channel{2}.jpeg'.format(repo, args.conv, f) img_noise = np.random.normal(size=(args.idim, args.idim, 3)) * 20 + 128 img_noise = img_noise.astype('float32') inp = transforms.ToTensor()(img_noise) inp = torch.unsqueeze(inp, 0) for it in range(args.niter): x = torch.autograd.Variable(inp.cuda(), requires_grad=True) out = forward(model, args.conv-1, f, x) criterion = nn.CrossEntropyLoss() filt_var = torch.autograd.Variable(torch.ones(1).long()*f).cuda() output = out.mean(3).mean(2) loss = - criterion(output, filt_var) - args.wd*torch.norm(x)**2 # compute gradient loss.backward() # normalize gradient grads = x.grad.data.cpu() grads = grads.div(torch.norm(grads)+1e-8) # apply gradient inp = inp.add(args.lr*grads) # gaussian blur if it%args.step == 0: inp = gaussian_filter(torch.squeeze(inp).numpy().transpose((2, 1, 0)), sigma=(args.sig, args.sig, 0)) inp = torch.unsqueeze(torch.from_numpy(inp).float().transpose(2, 0), 0) # save image at the last iteration if it == args.niter - 1: a = deprocess_image(inp.numpy()) Image.fromarray(a).save(fname_out) map(gradient_ascent, range(CONV[args.arch][args.conv-1])) def deprocess_image(x): x = x[0, :, :, :] # normalize tensor: center on 0., ensure std is 0.1 x -= x.mean() x /= (x.std() + 1e-5) x *= 0.1 # clip to [0, 1] x += 0.5 x = np.clip(x, 0, 1) # convert to RGB array x *= 255 x = x.transpose((1, 2, 0)) x = np.clip(x, 0, 255).astype('uint8') return x def forward(model, layer, channel, x): if model.sobel is not None: x = model.sobel(x) count = 0 for y, m in enumerate(model.features.modules()): if not isinstance(m, nn.Sequential): x = m(x) if isinstance(m, nn.Conv2d): if count == layer: res = x if isinstance(m, nn.ReLU): if count == layer: # check if channel is not activated if x[:, channel, :, :].mean().data.cpu().numpy() == 0: return res return x count = count + 1 if __name__ == '__main__': main()
deepcluster-main
visu/gradient_ascent.py
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # import argparse import os from shutil import copyfile import sys import numpy as np from PIL import Image import torch import torch.nn as nn import torchvision.transforms as transforms import torchvision.datasets as datasets sys.path.insert(0, '..') from util import load_model def parse_args(): parser = argparse.ArgumentParser(description='Retrieve images with maximal activations') parser.add_argument('--data', type=str, help='path to dataset') parser.add_argument('--model', type=str, help='Model') parser.add_argument('--conv', type=int, default=1, help='convolutional layer') parser.add_argument('--exp', type=str, default='', help='path to res') parser.add_argument('--count', type=int, default=9, help='save this many images') parser.add_argument('--workers', default=4, type=int, help='number of data loading workers (default: 4)') return parser.parse_args() def main(args): # create repo repo = os.path.join(args.exp, 'conv' + str(args.conv)) if not os.path.isdir(repo): os.makedirs(repo) # build model model = load_model(args.model) model.cuda() for params in model.parameters(): params.requires_grad = False model.eval() #load data normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) tra = [transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize] # dataset dataset = datasets.ImageFolder(args.data, transform=transforms.Compose(tra)) dataloader = torch.utils.data.DataLoader(dataset, batch_size=256, num_workers=args.workers) # keys are filters and value are arrays with activation scores for the whole dataset layers_activations = {} for i, (input_tensor, _) in enumerate(dataloader): input_var = torch.autograd.Variable(input_tensor.cuda(), volatile=True) activations = forward(model, args.conv, input_var) if i == 0: layers_activations = {filt: np.zeros(len(dataset)) for filt in activations} if i < len(dataloader) - 1: e_idx = (i + 1) * 256 else: e_idx = len(dataset) s_idx = i * 256 for filt in activations: layers_activations[filt][s_idx: e_idx] = activations[filt].cpu().data.numpy() if i % 100 == 0: print('{0}/{1}'.format(i, len(dataloader))) # save top N images for each filter for filt in layers_activations: repofilter = os.path.join(repo, filt) if not os.path.isdir(repofilter): os.mkdir(repofilter) top = np.argsort(layers_activations[filt])[::-1] if args.count > 0: top = top[:args.count] for pos, img in enumerate(top): src, _ = dataset.imgs[img] copyfile(src, os.path.join(repofilter, "{}_{}".format(pos, src.split('/')[-1]))) def forward(model, my_layer, x): if model.sobel is not None: x = model.sobel(x) layer = 1 res = {} for m in model.features.modules(): if not isinstance(m, nn.Sequential): x = m(x) if isinstance(m, nn.ReLU): if layer == my_layer: for channel in range(int(x.size()[1])): key = 'layer' + str(layer) + '-channel' + str(channel) res[key] = torch.squeeze(x.mean(3).mean(2))[:, channel] return res layer = layer + 1 return res if __name__ == '__main__': args = parse_args() main(args)
deepcluster-main
visu/activ-retrieval.py
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # from .vgg16 import * from .alexnet import *
deepcluster-main
models/__init__.py
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # import torch import torch.nn as nn import math from random import random as rd __all__ = [ 'VGG', 'vgg16'] class VGG(nn.Module): def __init__(self, features, num_classes, sobel): super(VGG, self).__init__() self.features = features self.classifier = nn.Sequential( nn.Linear(512 * 7 * 7, 4096), nn.ReLU(True), nn.Dropout(0.5), nn.Linear(4096, 4096), nn.ReLU(True) ) self.top_layer = nn.Linear(4096, num_classes) self._initialize_weights() if sobel: grayscale = nn.Conv2d(3, 1, kernel_size=1, stride=1, padding=0) grayscale.weight.data.fill_(1.0 / 3.0) grayscale.bias.data.zero_() sobel_filter = nn.Conv2d(1, 2, kernel_size=3, stride=1, padding=1) sobel_filter.weight.data[0,0].copy_( torch.FloatTensor([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) ) sobel_filter.weight.data[1,0].copy_( torch.FloatTensor([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) ) sobel_filter.bias.data.zero_() self.sobel = nn.Sequential(grayscale, sobel_filter) for p in self.sobel.parameters(): p.requires_grad = False else: self.sobel = None def forward(self, x): if self.sobel: x = self.sobel(x) x = self.features(x) x = x.view(x.size(0), -1) x = self.classifier(x) if self.top_layer: x = self.top_layer(x) return x def _initialize_weights(self): for y,m in enumerate(self.modules()): if isinstance(m, nn.Conv2d): #print(y) n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels for i in range(m.out_channels): m.weight.data[i].normal_(0, math.sqrt(2. / n)) if m.bias is not None: m.bias.data.zero_() elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() elif isinstance(m, nn.Linear): m.weight.data.normal_(0, 0.01) m.bias.data.zero_() def make_layers(input_dim, batch_norm): layers = [] in_channels = input_dim cfg = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'] for v in cfg: if v == 'M': layers += [nn.MaxPool2d(kernel_size=2, stride=2)] else: conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1) if batch_norm: layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)] else: layers += [conv2d, nn.ReLU(inplace=True)] in_channels = v return nn.Sequential(*layers) def vgg16(sobel=False, bn=True, out=1000): dim = 2 + int(not sobel) model = VGG(make_layers(dim, bn), out, sobel) return model
deepcluster-main
models/vgg16.py
# Copyright (c) 2017-present, Facebook, Inc. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # import math import numpy as np import torch import torch.nn as nn __all__ = [ 'AlexNet', 'alexnet'] # (number of filters, kernel size, stride, pad) CFG = { '2012': [(96, 11, 4, 2), 'M', (256, 5, 1, 2), 'M', (384, 3, 1, 1), (384, 3, 1, 1), (256, 3, 1, 1), 'M'] } class AlexNet(nn.Module): def __init__(self, features, num_classes, sobel): super(AlexNet, self).__init__() self.features = features self.classifier = nn.Sequential(nn.Dropout(0.5), nn.Linear(256 * 6 * 6, 4096), nn.ReLU(inplace=True), nn.Dropout(0.5), nn.Linear(4096, 4096), nn.ReLU(inplace=True)) self.top_layer = nn.Linear(4096, num_classes) self._initialize_weights() if sobel: grayscale = nn.Conv2d(3, 1, kernel_size=1, stride=1, padding=0) grayscale.weight.data.fill_(1.0 / 3.0) grayscale.bias.data.zero_() sobel_filter = nn.Conv2d(1, 2, kernel_size=3, stride=1, padding=1) sobel_filter.weight.data[0, 0].copy_( torch.FloatTensor([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) ) sobel_filter.weight.data[1, 0].copy_( torch.FloatTensor([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) ) sobel_filter.bias.data.zero_() self.sobel = nn.Sequential(grayscale, sobel_filter) for p in self.sobel.parameters(): p.requires_grad = False else: self.sobel = None def forward(self, x): if self.sobel: x = self.sobel(x) x = self.features(x) x = x.view(x.size(0), 256 * 6 * 6) x = self.classifier(x) if self.top_layer: x = self.top_layer(x) return x def _initialize_weights(self): for y, m in enumerate(self.modules()): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels for i in range(m.out_channels): m.weight.data[i].normal_(0, math.sqrt(2. / n)) if m.bias is not None: m.bias.data.zero_() elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() elif isinstance(m, nn.Linear): m.weight.data.normal_(0, 0.01) m.bias.data.zero_() def make_layers_features(cfg, input_dim, bn): layers = [] in_channels = input_dim for v in cfg: if v == 'M': layers += [nn.MaxPool2d(kernel_size=3, stride=2)] else: conv2d = nn.Conv2d(in_channels, v[0], kernel_size=v[1], stride=v[2], padding=v[3]) if bn: layers += [conv2d, nn.BatchNorm2d(v[0]), nn.ReLU(inplace=True)] else: layers += [conv2d, nn.ReLU(inplace=True)] in_channels = v[0] return nn.Sequential(*layers) def alexnet(sobel=False, bn=True, out=1000): dim = 2 + int(not sobel) model = AlexNet(make_layers_features(CFG['2012'], dim, bn=bn), out, sobel) return model
deepcluster-main
models/alexnet.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # import argparse import csv from pathlib import Path import pandas as pd XX_EN_LANGUAGES = { "1": ["fr", "de", "nl", "ru", "es", "it", "tr", "fa", "sv-SE", "mn", "zh-CN"], "2": ["fr", "de", "es", "ca", "it", "ru", "zh-CN", "pt", "fa", "et", "mn", "nl", "tr", "ar", "sv-SE", "lv", "sl", "ta", "ja", "id", "cy"] } EN_XX_LANGUAGES = { "1": [], "2": ["de", "tr", "fa", "sv-SE", "mn", "zh-CN", "cy", "ca", "sl", "et", "id", "ar", "ta", "lv", "ja"] } SPLITS = ["train", "dev", "test"] TSV_PREFIX = {"1": "covost", "2": "covost_v2"} def get_args(): parser = argparse.ArgumentParser() parser.add_argument("--version", "-v", type=str, choices=["1", "2"], required=True, help="CoVoST version") parser.add_argument("--src-lang", "-s", type=str, required=True, help="source language code") parser.add_argument("--tgt-lang", "-t", type=str, required=True, help="target language code") parser.add_argument("--root", "-d", type=str, required=True, help="root path to translation TSV and output TSVs") parser.add_argument("--cv-tsv", type=str, required=True, help="path to validated.tsv from Common Voice") return parser.parse_args() def load_df_from_tsv(path: Path): return pd.read_csv(path, sep="\t", header=0, encoding="utf-8", escapechar="\\", quoting=csv.QUOTE_NONE, na_filter=False) def save_df_to_tsv(dataframe, path: Path): dataframe.to_csv(path, sep="\t", header=True, index=False, encoding="utf-8", escapechar="\\", quoting=csv.QUOTE_NONE) def get_v1_split(df: pd.DataFrame, split: str): return df[(df["split"] == split) | (df["split"] == f"{split}_covost")] def get_v2_split(df: pd.DataFrame, split: str): if split == "train": return df[(df["split"] == split) | (df["split"] == f"{split}_covost")] else: return df[df["split"] == split] def main(): args = get_args() ver, src, tgt = args.version, args.src_lang, args.tgt_lang assert src != tgt and "en" in {src, tgt} if src == "en": assert tgt in EN_XX_LANGUAGES[ver] else: assert src in XX_EN_LANGUAGES[ver] root = Path(args.root) tsv_prefix = TSV_PREFIX[ver] cv_tsv = load_df_from_tsv(args.cv_tsv) covost_tsv = load_df_from_tsv(root / f"{tsv_prefix}.{src}_{tgt}.tsv") df = pd.merge( left=cv_tsv[["path", "sentence", "client_id"]], right=covost_tsv[["path", "translation", "split"]], how="inner", on="path", ) for split in SPLITS: get_split = {"1": get_v1_split, "2": get_v2_split}.get(ver, None) assert get_split is not None cur_df = get_split(df, split) cur_df = cur_df[["path", "sentence", "translation", "client_id"]] save_df_to_tsv(cur_df, root / f"{tsv_prefix}.{src}_{tgt}.{split}.tsv") if __name__ == "__main__": main()
covost-main
get_covost_splits.py
# Copyright (c) Facebook, Inc. and its affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # import argparse import os import os.path as op import urllib.request from tqdm import tqdm LANG_CODE_2_TO_3 = { 'fr': 'fra', 'de': 'deu', 'nl': 'nld', 'ru': 'rus', 'en': 'eng', 'es': 'spa' } def get_args(): parser = argparse.ArgumentParser() parser.add_argument('--root', type=str, default='data/tt/mp3', help='root path for MP3 files') return parser.parse_args() def _download_mp3(root: str, lang: str, s_id: str, overwrite=False): path = op.join(root, f'{s_id}.mp3') if not overwrite and op.isfile(path): return url = f'https://audio.tatoeba.org/sentences/{lang}/{s_id}.mp3' try: urllib.request.urlretrieve(url, path) except Exception as e: print(e, url) return str(e) def main(): args = get_args() if not op.isdir(args.root): os.makedirs(args.root) for lang in LANG_CODE_2_TO_3: print(f'Downloading {lang} speeches...') lang_3 = LANG_CODE_2_TO_3[lang] with open(f'data/tt/tatoeba20191004.s2t.{lang}_en.tsv') as f: next(f) ids = [r.strip().split('\t')[0] for r in f] for i in tqdm(ids): _download_mp3(args.root, lang_3, i) if __name__ == '__main__': main()
covost-main
get_tt_speech.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. ### useage ### # (run w/ gpu): python dempStream.py --tx_cuda 1 --rx_cuda 2 --model libritts_v1 --input_device x --output_device o # (run w/ cpu): python dempStream.py --tx_cuda -1 --rx_cuda -1 --model libritts_sym --input_device x --output_device o import torch import argparse from utils.audiodec import AudioDec, AudioDecStreamer, assign_model def main(): parser = argparse.ArgumentParser() parser.add_argument("--model", type=str, default="libritts_sym") parser.add_argument("-i", "--input", type=str, default="input.wav") parser.add_argument("-o", "--output", type=str, default="output.wav") parser.add_argument('--tx_cuda', type=int, default=-1 ) parser.add_argument('--rx_cuda', type=int, default=-1 ) parser.add_argument('--input_device', type=int, default=1) parser.add_argument('--output_device', type=int, default=4) parser.add_argument('--frame_size', type=int, default=1200) parser.add_argument('--num_threads', type=int, default=4) args = parser.parse_args() # device assignment if args.tx_cuda < 0: tx_device = f'cpu' else: tx_device = f'cuda:{args.tx_cuda}' if args.rx_cuda < 0: rx_device = f'cpu' else: rx_device = f'cuda:{args.rx_cuda}' torch.set_num_threads(args.num_threads) # model assignment sample_rate, encoder_checkpoint, decoder_checkpoint = assign_model(args.model) # AudioDec initinalize print("AudioDec initinalizing!") audiodec = AudioDec(tx_device=tx_device, rx_device=rx_device) audiodec.load_transmitter(encoder_checkpoint) audiodec.load_receiver(encoder_checkpoint, decoder_checkpoint) # Streamer initinalize print("Streamer initinalizing!") streamer = AudioDecStreamer( input_device=args.input_device, output_device=args.output_device, frame_size=args.frame_size, sample_rate=sample_rate, tx_encoder=audiodec.tx_encoder, tx_device=tx_device, rx_encoder=audiodec.rx_encoder, decoder=audiodec.decoder, rx_device=rx_device, ) streamer.enable_filedump( input_stream_file=args.input, output_stream_file=args.output, ) # run print("Ready to run!") latency="low" # TODO this is responsible for ~100ms latency, seems to be driver dependent. latency=0 works on Mac but not on Windows streamer.run(latency) if __name__ == "__main__": main()
AudioDec-main
demoStream.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. ### useage ### # (run w/ gpu): python demoFile.py --model libritts_v1 -i xxx.wav -o ooo.wav # (run w/ cpu): python demoFile.py --cuda -1 --model libritts_sym -i xxx.wav -o ooo.wav import os import torch import argparse import numpy as np import soundfile as sf from utils.audiodec import AudioDec, assign_model def main(): parser = argparse.ArgumentParser() parser.add_argument("--model", type=str, default="libritts_v1") parser.add_argument("-i", "--input", type=str, required=True) parser.add_argument("-o", "--output", type=str, required=True) parser.add_argument('--cuda', type=int, default=0 ) parser.add_argument('--num_threads', type=int, default=4) args = parser.parse_args() # device assignment if args.cuda < 0: tx_device = f'cpu' rx_device = f'cpu' else: tx_device = f'cuda:{args.cuda}' rx_device = f'cuda:{args.cuda}' torch.set_num_threads(args.num_threads) # model assignment sample_rate, encoder_checkpoint, decoder_checkpoint = assign_model(args.model) # AudioDec initinalize print("AudioDec initinalizing!") audiodec = AudioDec(tx_device=tx_device, rx_device=rx_device) audiodec.load_transmitter(encoder_checkpoint) audiodec.load_receiver(encoder_checkpoint, decoder_checkpoint) with torch.no_grad(): if os.path.exists(args.input): data, fs = sf.read(args.input, always_2d=True) else: raise ValueError(f'Input file {args.input} does not exist!') assert fs == sample_rate, f"data ({fs}Hz) is not matched to model ({sample_rate}Hz)!" x = np.expand_dims(data.transpose(1, 0), axis=1) # (T, C) -> (C, 1, T) x = torch.tensor(x, dtype=torch.float).to(tx_device) print("Encode/Decode...") z = audiodec.tx_encoder.encode(x) idx = audiodec.tx_encoder.quantize(z) zq = audiodec.rx_encoder.lookup(idx) y = audiodec.decoder.decode(zq)[:, :, :x.size(-1)] y = y.squeeze(1).transpose(1, 0).cpu().numpy() # T x C sf.write( args.output, y, fs, "PCM_16", ) print(f"Output {args.output}!") if __name__ == "__main__": main()
AudioDec-main
demoFile.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # # Reference (https://github.com/kan-bayashi/ParallelWaveGAN/) import os import sys import yaml import torch import logging import argparse import numpy as np import soundfile as sf from tqdm import tqdm from sklearn.preprocessing import StandardScaler from dataloader import SingleDataset from models.autoencoder.AudioDec import Generator as generator_audiodec class StatisticMain(object): def __init__(self, args,): # set logger logging.basicConfig( level=logging.INFO, stream=sys.stdout, format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s", ) # device if not torch.cuda.is_available(): self.device = torch.device('cpu') logging.info(f"device: cpu") else: self.device = torch.device('cuda') logging.info(f"device: gpu") # initialize config with open(args.config, 'r') as f: self.config = yaml.load(f, Loader=yaml.FullLoader) # initialize attribute self.stats_path = self.config['stats'] self.analyzer_checkpoint = self.config['analyzer'] self.analyzer_config = self._load_config(self.analyzer_checkpoint) self.model_type = self.analyzer_config.get('model_type', 'symAudioDec') os.makedirs(os.path.dirname(self.stats_path), exist_ok=True) def _load_config(self, checkpoint, config_name='config.yml'): dirname = os.path.dirname(checkpoint) config_path = os.path.join(dirname, config_name) with open(config_path) as f: config = yaml.load(f, Loader=yaml.Loader) return config def load_dataset(self, subset, subset_num): audio_path = os.path.join( self.config['data']['path'], self.config['data']['subset'][subset], ) assert os.path.exists(audio_path), f"{audio_path} does not exist!" self.dataset = SingleDataset( files=audio_path, query="*.wav", load_fn=sf.read, return_utt_id=False, subset_num=subset_num, ) logging.info(f"The number of {subset} audio files = {len(self.dataset)}.") def load_analyzer(self): if self.model_type in ['symAudioDec', 'symAudioDecUniv']: analyzer = generator_audiodec else: raise NotImplementedError(f"Analyzer {self.model_type} is not supported!") self.analyzer = analyzer(**self.analyzer_config['generator_params']) self.analyzer.load_state_dict( torch.load(self.analyzer_checkpoint, map_location='cpu')['model']['generator']) self.analyzer = self.analyzer.eval().to(self.device) logging.info(f"Loaded Analyzer from {self.analyzer_checkpoint}.") def audio_analysis(self, audio): x = torch.tensor(audio, dtype=torch.float).to(self.device) x = x.transpose(1, 0).unsqueeze(0) # (T, C) -> (1, C, T) x = self.analyzer.encoder(x) z = self.analyzer.projector(x) zq, _, _ = self.analyzer.quantizer(z) return zq.squeeze(0).transpose(1, 0).cpu().numpy() # (T', C) def run(self): with torch.no_grad(), tqdm(self.dataset, desc="[statistic]") as pbar: scaler = StandardScaler() for idx, x in enumerate(pbar, 1): zq = self.audio_analysis(x) scaler.partial_fit(zq) stats = np.stack([scaler.mean_, scaler.scale_], axis=0) np.save( self.stats_path, stats.astype(np.float32), allow_pickle=False, ) logging.info(f"Finished statistical calculation of {idx} utterances.") def main(): """Run feature extraction process.""" parser = argparse.ArgumentParser() parser.add_argument('-c', '--config', type=str, required=True) parser.add_argument("--subset", type=str, default="train") parser.add_argument("--subset_num", type=int, default=-1) args = parser.parse_args() # initial statistic_main statistic_main = StatisticMain(args=args) # load dataset statistic_main.load_dataset(args.subset, args.subset_num) # load analyzer statistic_main.load_analyzer() # run testing statistic_main.run() if __name__ == "__main__": main()
AudioDec-main
codecStatistic.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # # Reference (https://github.com/kan-bayashi/ParallelWaveGAN/) import os import logging import argparse import torch import soundfile as sf from torch.utils.data import DataLoader from dataloader import CollaterAudio, CollaterAudioPair from dataloader import SingleDataset, MultiDataset from models.autoencoder.AudioDec import Generator as generator_audiodec from models.vocoder.HiFiGAN import Generator as generator_hifigan from models.vocoder.HiFiGAN import Discriminator as discriminator_hifigan from models.vocoder.UnivNet import Discriminator as discriminator_univnet from trainer.autoencoder import Trainer as TrainerAutoEncoder from trainer.vocoder import Trainer as TrainerVocoder from trainer.denoise import Trainer as TrainerDenoise from bin.train import TrainGAN from losses import DiscriminatorAdversarialLoss from losses import FeatureMatchLoss from losses import GeneratorAdversarialLoss from losses import MultiResolutionSTFTLoss from losses import MultiMelSpectrogramLoss from losses import MultiWindowShapeLoss class TrainMain(TrainGAN): def __init__(self, args,): super(TrainMain, self).__init__(args=args,) self.train_mode = self.config.get('train_mode', 'autoencoder') self.model_type = self.config.get('model_type', 'symAudioDec') self.data_path = self.config['data']['path'] # DATA LOADER def initialize_data_loader(self): logging.info(f"Loading datasets... (batch_lenght: {self.batch_length})") if self.train_mode in ['autoencoder', 'vocoder']: train_set = self._audio('train') valid_set = self._audio('valid') collater = CollaterAudio(batch_length=self.batch_length) elif self.train_mode in ['denoise']: train_set = self._audio_pair('noisy_train', 'clean_train') valid_set = self._audio_pair('noisy_valid', 'clean_valid') collater = CollaterAudioPair(batch_length=self.batch_length) else: raise NotImplementedError(f"Train mode: {self.train_mode} is not supported!") logging.info(f"The number of training files = {len(train_set)}.") logging.info(f"The number of validation files = {len(valid_set)}.") dataset = {'train': train_set, 'dev': valid_set} self._data_loader(dataset, collater) def _data_loader(self, dataset, collater): self.data_loader = { 'train': DataLoader( dataset=dataset['train'], shuffle=True, collate_fn=collater, batch_size=self.config['batch_size'], num_workers=self.config['num_workers'], pin_memory=self.config['pin_memory'], ), 'dev': DataLoader( dataset=dataset['dev'], shuffle=False, collate_fn=collater, batch_size=self.config['batch_size'], num_workers=self.config['num_workers'], pin_memory=self.config['pin_memory'], ), } def _audio(self, subset, subset_num=-1, return_utt_id=False): audio_dir = os.path.join( self.data_path, self.config['data']['subset'][subset]) params = { 'files': audio_dir, 'query': "*.wav", 'load_fn': sf.read, 'return_utt_id': return_utt_id, 'subset_num': subset_num, } return SingleDataset(**params) def _audio_pair(self, subset_n, subset_c, subset_num=-1, return_utt_id=False): audio_n_dir = os.path.join( self.data_path, self.config['data']['subset'][subset_n]) audio_c_dir = os.path.join( self.data_path, self.config['data']['subset'][subset_c]) params = { 'multi_files': [audio_c_dir, audio_n_dir], # (main, sub) 'queries': ["*.wav"]*2, 'load_fns': [sf.read]*2, 'return_utt_id': return_utt_id, 'subset_num': subset_num, } return MultiDataset(**params) # MODEL ARCHITECTURE def define_model(self): # generator generator = self._define_generator(self.model_type) self.model['generator'] = generator.to(self.device) # discriminator discriminator = self._define_discriminator(self.model_type) self.model['discriminator'] = discriminator.to(self.device) # optimizer self._define_optimizer_scheduler() #self._show_setting() def _define_generator(self, model_type): if model_type in ['symAudioDec', 'symAudioDecUniv']: generator = generator_audiodec elif model_type in ['HiFiGAN', 'UnivNet']: generator = generator_hifigan else: raise NotImplementedError(f"Model type: {model_type} is not supported for the generator!") return generator(**self.config['generator_params']) def _define_discriminator(self, model_type): if model_type in ['symAudioDec', 'HiFiGAN']: discriminator = discriminator_hifigan elif model_type in ['symAudioDecUniv', 'UnivNet']: discriminator = discriminator_univnet else: raise NotImplementedError(f"Model type: {model_type} is not supported for the discriminator!") return discriminator(**self.config['discriminator_params']) def _define_optimizer_scheduler(self): generator_optimizer_class = getattr( torch.optim, self.config['generator_optimizer_type'] ) discriminator_optimizer_class = getattr( torch.optim, self.config['discriminator_optimizer_type'] ) self.optimizer = { 'generator': generator_optimizer_class( self.model['generator'].parameters(), **self.config['generator_optimizer_params'], ), 'discriminator': discriminator_optimizer_class( self.model['discriminator'].parameters(), **self.config['discriminator_optimizer_params'], ), } generator_scheduler_class = getattr( torch.optim.lr_scheduler, self.config.get('generator_scheduler_type', "StepLR"), ) discriminator_scheduler_class = getattr( torch.optim.lr_scheduler, self.config.get('discriminator_scheduler_type', "StepLR"), ) self.scheduler = { 'generator': generator_scheduler_class( optimizer=self.optimizer['generator'], **self.config['generator_scheduler_params'], ), 'discriminator': discriminator_scheduler_class( optimizer=self.optimizer['discriminator'], **self.config['discriminator_scheduler_params'], ), } # CRITERIA def define_criterion(self): self.criterion = { 'gen_adv': GeneratorAdversarialLoss( **self.config['generator_adv_loss_params']).to(self.device), 'dis_adv': DiscriminatorAdversarialLoss( **self.config['discriminator_adv_loss_params']).to(self.device), } if self.config.get('use_feat_match_loss', False): self.criterion['feat_match'] = FeatureMatchLoss( **self.config.get('feat_match_loss_params', {}), ).to(self.device) if self.config.get('use_mel_loss', False): self.criterion['mel'] = MultiMelSpectrogramLoss( **self.config['mel_loss_params'], ).to(self.device) if self.config.get('use_stft_loss', False): self.criterion['stft'] = MultiResolutionSTFTLoss( **self.config['stft_loss_params'], ).to(self.device) if self.config.get('use_shape_loss', False): self.criterion['shape'] = MultiWindowShapeLoss( **self.config['shape_loss_params'], ).to(self.device) # TRAINER def define_trainer(self): if self.train_mode in ['autoencoder']: trainer = TrainerAutoEncoder elif self.train_mode in ['vocoder']: trainer = TrainerVocoder elif self.train_mode in ['denoise']: trainer = TrainerDenoise else: raise NotImplementedError(f"Train mode: {self.train_mode} is not supported for Trainer!") trainer_parameters = {} trainer_parameters['steps'] = 0 trainer_parameters['epochs'] = 0 trainer_parameters['data_loader'] = self.data_loader trainer_parameters['model'] = self.model trainer_parameters['criterion'] = self.criterion trainer_parameters['optimizer'] = self.optimizer trainer_parameters['scheduler'] = self.scheduler trainer_parameters['config'] = self.config trainer_parameters['device'] = self.device self.trainer = trainer(**trainer_parameters) # MODEL INITIALIZATION def initialize_model(self): initial = self.config.get("initial", "") if os.path.exists(self.resume): # resume from trained model self.trainer.load_checkpoint(self.resume) logging.info(f"Successfully resumed from {self.resume}.") elif os.path.exists(initial): # initial new model with the pre-trained model self.trainer.load_checkpoint(initial, load_only_params=True) logging.info(f"Successfully initialize parameters from {initial}.") else: logging.info("Train from scrach") # load the pre-trained encoder for vocoder training if self.train_mode in ['vocoder']: analyzer_checkpoint = self.config.get("analyzer", "") assert os.path.exists(analyzer_checkpoint), f"Analyzer {analyzer_checkpoint} does not exist!" analyzer_config = self._load_config(analyzer_checkpoint) self._initialize_analyzer(analyzer_config, analyzer_checkpoint) def _initialize_analyzer(self, config, checkpoint): model_type = config.get('model_type', 'symAudioDec') if model_type in ['symAudioDec', 'symAudioDecUniv']: analyzer = generator_audiodec else: raise NotImplementedError(f"Model type: {model_type} is not supported for the analyzer!") self.model['analyzer'] = analyzer(**config['generator_params']).to(self.device) self.model['analyzer'].load_state_dict( torch.load(checkpoint, map_location='cpu')['model']['generator']) logging.info(f"Successfully load analyzer from {checkpoint}.") def main(): parser = argparse.ArgumentParser() parser.add_argument('-c', '--config', type=str, required=True) parser.add_argument("--tag", type=str, required=True) parser.add_argument("--exp_root", type=str, default="exp") parser.add_argument("--resume", default="", type=str, nargs="?", help='checkpoint file path to resume training. (default="")', ) parser.add_argument('--seed', default=1337, type=int) parser.add_argument('--disable_cudnn', choices=('True','False'), default='False', help='Disable CUDNN') args = parser.parse_args() # initial train_main train_main = TrainMain(args=args) # get dataset train_main.initialize_data_loader() # define models, optimizers, and schedulers train_main.define_model() # define criteria train_main.define_criterion() # define trainer train_main.define_trainer() # model initialization train_main.initialize_model() # run training loop train_main.run() if __name__ == "__main__": main()
AudioDec-main
codecTrain.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # # Reference (https://github.com/kan-bayashi/ParallelWaveGAN/) import os import torch import logging import argparse import soundfile as sf from dataloader import SingleDataset from models.autoencoder.AudioDec import Generator as generator_audiodec from models.vocoder.HiFiGAN import Generator as generator_hifigan from bin.test import TestGEN class TestMain(TestGEN): def __init__(self, args,): super(TestMain, self).__init__(args=args,) self.encoder_type = self.encoder_config.get('model_type', 'symAudioDec') self.decoder_type = self.decoder_config.get('model_type', 'symAudioDec') if self.encoder_config['generator_params']['input_channels'] > 1: self.multi_channel = True else: self.multi_channel = False # LOAD DATASET def load_dataset(self, subset, subset_num): data_path = os.path.join( self.encoder_config['data']['path'], self.encoder_config['data']['subset'][subset] ) assert os.path.exists(data_path), f"{data_path} does not exist!" self.dataset = SingleDataset( files=data_path, query="*.wav", load_fn=sf.read, return_utt_id=True, subset_num=subset_num, ) logging.info(f"The number of utterances = {len(self.dataset)}.") # LOAD MODEL def load_encoder(self): if self.encoder_type in ['symAudioDec', 'symAudioDecUniv']: encoder = generator_audiodec else: raise NotImplementedError(f"Encoder {self.encoder_type} is not supported!") self.encoder = encoder(**self.encoder_config['generator_params']) self.encoder.load_state_dict( torch.load(self.encoder_checkpoint, map_location='cpu')['model']['generator']) self.encoder = self.encoder.eval().to(self.device) logging.info(f"Loaded Encoder from {self.encoder_checkpoint}.") def load_decoder(self): if self.decoder_type in ['symAudioDec', 'symAudioDecUniv']: decoder = generator_audiodec elif self.decoder_type in ['HiFiGAN', 'UnivNet']: decoder = generator_hifigan else: raise NotImplementedError(f"Decoder {self.decoder_type} is not supported!") self.decoder = decoder(**self.decoder_config['generator_params']) self.decoder.load_state_dict( torch.load(self.decoder_checkpoint, map_location='cpu')['model']['generator']) self.decoder = self.decoder.eval().to(self.device) logging.info(f"Loaded Decoder from {self.decoder_checkpoint}.") def encode(self, audio): x = torch.tensor(audio, dtype=torch.float).to(self.device) if self.multi_channel: x = x.transpose(1, 0).unsqueeze(0) # (T, C) -> (1, C, T) else: x = x.transpose(1, 0).unsqueeze(1) # (T, C) -> (C, 1, T) x = self.encoder.encoder(x) z = self.encoder.projector(x) zq, _, _ = self.encoder.quantizer(z) return zq def decode(self, zq): if self.decoder_type in ['HiFiGAN', 'UnivNet']: y = self.decoder(zq) else: y = self.decoder.decoder(zq) return y # INITIAL FOLDER def initial_folder(self, subset, output_name): # model name encoder = os.path.dirname(self.encoder_checkpoint).split('/')[-1] decoder = os.path.dirname(self.decoder_checkpoint).split('/')[-1] # model checkpoint encoder_checkpoint = os.path.basename(self.encoder_checkpoint).split('steps')[0].split('-')[-1] decoder_checkpoint = os.path.basename(self.decoder_checkpoint).split('steps')[0].split('-')[-1] testdir = f"{encoder}-{decoder}_{encoder_checkpoint}-{decoder_checkpoint}" # testing set setdir = self.encoder_config['data']['subset'][subset] self.outdir = os.path.join(output_name, testdir, setdir) if not os.path.exists(self.outdir): os.makedirs(self.outdir, exist_ok=True) def main(): """Run testing process.""" parser = argparse.ArgumentParser() parser.add_argument("--subset", type=str, default="clean_test") parser.add_argument("--subset_num", type=int, default=-1) parser.add_argument("--encoder", type=str, required=True) parser.add_argument("--decoder", type=str, required=True) parser.add_argument("--output_dir", type=str, required=True) args = parser.parse_args() # initial test_main test_main = TestMain(args=args) # load dataset test_main.load_dataset(args.subset, args.subset_num) # load model test_main.load_encoder() test_main.load_decoder() # initial folder test_main.initial_folder(args.subset, args.output_dir) # run testing test_main.run() if __name__ == "__main__": main()
AudioDec-main
codecTest.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright 2021 Tomoki Hayashi # MIT License (https://opensource.org/licenses/MIT) """Adversarial loss modules.""" import torch import torch.nn.functional as F class GeneratorAdversarialLoss(torch.nn.Module): """Generator adversarial loss module.""" def __init__( self, average_by_discriminators=True, loss_type="mse", ): """Initialize GeneratorAversarialLoss module.""" super().__init__() self.average_by_discriminators = average_by_discriminators assert loss_type in ["mse", "hinge"], f"{loss_type} is not supported." if loss_type == "mse": self.criterion = self._mse_loss else: self.criterion = self._hinge_loss def forward(self, outputs): """Calcualate generator adversarial loss. Args: outputs (Tensor or list): Discriminator outputs or list of discriminator outputs. Returns: Tensor: Generator adversarial loss value. """ if isinstance(outputs, (tuple, list)): adv_loss = 0.0 for i, outputs_ in enumerate(outputs): if isinstance(outputs_, (tuple, list)): # NOTE(kan-bayashi): case including feature maps outputs_ = outputs_[-1] adv_loss += self.criterion(outputs_) if self.average_by_discriminators: adv_loss /= i + 1 else: adv_loss = self.criterion(outputs) return adv_loss def _mse_loss(self, x): return F.mse_loss(x, x.new_ones(x.size())) def _hinge_loss(self, x): return -x.mean() class DiscriminatorAdversarialLoss(torch.nn.Module): """Discriminator adversarial loss module.""" def __init__( self, average_by_discriminators=True, loss_type="mse", ): """Initialize DiscriminatorAversarialLoss module.""" super().__init__() self.average_by_discriminators = average_by_discriminators assert loss_type in ["mse", "hinge"], f"{loss_type} is not supported." if loss_type == "mse": self.fake_criterion = self._mse_fake_loss self.real_criterion = self._mse_real_loss else: self.fake_criterion = self._hinge_fake_loss self.real_criterion = self._hinge_real_loss def forward(self, outputs_hat, outputs): """Calcualate discriminator adversarial loss. Args: outputs_hat (Tensor or list): Discriminator outputs or list of discriminator outputs calculated from generator outputs. outputs (Tensor or list): Discriminator outputs or list of discriminator outputs calculated from groundtruth. Returns: Tensor: Discriminator real loss value. Tensor: Discriminator fake loss value. """ if isinstance(outputs, (tuple, list)): real_loss = 0.0 fake_loss = 0.0 for i, (outputs_hat_, outputs_) in enumerate(zip(outputs_hat, outputs)): if isinstance(outputs_hat_, (tuple, list)): # NOTE(kan-bayashi): case including feature maps outputs_hat_ = outputs_hat_[-1] outputs_ = outputs_[-1] real_loss += self.real_criterion(outputs_) fake_loss += self.fake_criterion(outputs_hat_) if self.average_by_discriminators: fake_loss /= i + 1 real_loss /= i + 1 else: real_loss = self.real_criterion(outputs) fake_loss = self.fake_criterion(outputs_hat) return real_loss, fake_loss def _mse_real_loss(self, x): return F.mse_loss(x, x.new_ones(x.size())) def _mse_fake_loss(self, x): return F.mse_loss(x, x.new_zeros(x.size())) def _hinge_real_loss(self, x): return -torch.mean(torch.min(x - 1, x.new_zeros(x.size()))) def _hinge_fake_loss(self, x): return -torch.mean(torch.min(-x - 1, x.new_zeros(x.size())))
AudioDec-main
losses/adversarial_loss.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # # Reference (https://github.com/kan-bayashi/ParallelWaveGAN/) """STFT-based loss modules.""" import torch import torch.nn.functional as F def stft(x, fft_size, hop_size, win_length, window, eps=1e-7): """Perform STFT and convert to magnitude spectrogram. Args: x (Tensor): Input signal tensor (B, T). fft_size (int): FFT size. hop_size (int): Hop size. win_length (int): Window length. window (str): Window function type. Returns: Tensor: Magnitude spectrogram (B, #frames, fft_size // 2 + 1). """ x_stft = torch.stft(x, fft_size, hop_size, win_length, window, return_complex=True) x_power = x_stft.real ** 2 + x_stft.imag ** 2 return torch.sqrt(torch.clamp(x_power, min=eps)).transpose(2, 1) class SpectralConvergenceLoss(torch.nn.Module): """Spectral convergence loss module.""" def __init__(self): """Initilize spectral convergence loss module.""" super(SpectralConvergenceLoss, self).__init__() def forward(self, x_mag, y_mag): """Calculate forward propagation. Args: x_mag (Tensor): Magnitude spectrogram of predicted signal (B, #frames, #freq_bins). y_mag (Tensor): Magnitude spectrogram of groundtruth signal (B, #frames, #freq_bins). Returns: Tensor: Spectral convergence loss value. """ return torch.norm(y_mag - x_mag, p="fro") / torch.norm(y_mag, p="fro") class LogSTFTMagnitudeLoss(torch.nn.Module): """Log STFT magnitude loss module.""" def __init__(self): """Initilize los STFT magnitude loss module.""" super(LogSTFTMagnitudeLoss, self).__init__() def forward(self, x_mag, y_mag): """Calculate forward propagation. Args: x_mag (Tensor): Magnitude spectrogram of predicted signal (B, #frames, #freq_bins). y_mag (Tensor): Magnitude spectrogram of groundtruth signal (B, #frames, #freq_bins). Returns: Tensor: Log STFT magnitude loss value. """ return F.l1_loss(torch.log(y_mag), torch.log(x_mag)) class STFTLoss(torch.nn.Module): """STFT loss module.""" def __init__( self, fft_size=1024, hop_size=120, win_length=600, window="hann_window", ): """Initialize STFT loss module.""" super(STFTLoss, self).__init__() self.fft_size = fft_size self.hop_size = hop_size self.win_length = win_length self.spectral_convergence_loss = SpectralConvergenceLoss() self.log_stft_magnitude_loss = LogSTFTMagnitudeLoss() self.register_buffer("window", getattr(torch, window)(win_length)) def forward(self, x, y): """Calculate forward propagation. Args: x (Tensor): Predicted signal (B, T). y (Tensor): Groundtruth signal (B, T). Returns: Tensor: Spectral convergence loss value. Tensor: Log STFT magnitude loss value. """ x_mag = stft(x, self.fft_size, self.hop_size, self.win_length, self.window) y_mag = stft(y, self.fft_size, self.hop_size, self.win_length, self.window) sc_loss = self.spectral_convergence_loss(x_mag, y_mag) mag_loss = self.log_stft_magnitude_loss(x_mag, y_mag) return sc_loss, mag_loss class MultiResolutionSTFTLoss(torch.nn.Module): """Multi resolution STFT loss module.""" def __init__( self, fft_sizes=[1024, 2048, 512], hop_sizes=[120, 240, 50], win_lengths=[600, 1200, 240], window="hann_window", ): """Initialize Multi resolution STFT loss module. Args: fft_sizes (list): List of FFT sizes. hop_sizes (list): List of hop sizes. win_lengths (list): List of window lengths. window (str): Window function type. """ super(MultiResolutionSTFTLoss, self).__init__() assert len(fft_sizes) == len(hop_sizes) == len(win_lengths) self.stft_losses = torch.nn.ModuleList() for fft_size, hop_size, win_length in zip(fft_sizes, hop_sizes, win_lengths): self.stft_losses += [STFTLoss(fft_size, hop_size, win_length, window)] def forward(self, x, y): """Calculate forward propagation. Args: x (Tensor): Predicted signal (B, T) or (B, #subband, T). y (Tensor): Groundtruth signal (B, T) or (B, #subband, T). Returns: Tensor: Multi resolution spectral convergence loss value. Tensor: Multi resolution log STFT magnitude loss value. """ if len(x.shape) == 3: x = x.view(-1, x.size(2)) # (B, C, T) -> (B x C, T) y = y.view(-1, y.size(2)) # (B, C, T) -> (B x C, T) sc_loss = 0.0 mag_loss = 0.0 for f in self.stft_losses: sc_l, mag_l = f(x, y) sc_loss += sc_l mag_loss += mag_l sc_loss /= len(self.stft_losses) mag_loss /= len(self.stft_losses) return sc_loss, mag_loss
AudioDec-main
losses/stft_loss.py
from .adversarial_loss import * # NOQA from .feat_match_loss import * # NOQA from .mel_loss import * # NOQA from .stft_loss import * # NOQA from .waveform_loss import * # NOQA
AudioDec-main
losses/__init__.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # # Reference (https://github.com/kan-bayashi/ParallelWaveGAN/) """Mel-spectrogram loss modules.""" import librosa import torch import torch.nn.functional as F class MelSpectrogram(torch.nn.Module): """Calculate Mel-spectrogram.""" def __init__( self, fs=22050, fft_size=1024, hop_size=256, win_length=None, window="hann_window", num_mels=80, fmin=80, fmax=7600, center=True, normalized=False, onesided=True, eps=1e-10, log_base=10.0, ): """Initialize MelSpectrogram module.""" super().__init__() self.fft_size = fft_size self.hop_size = hop_size if win_length is not None: self.win_length = win_length else: self.win_length = fft_size self.center = center self.normalized = normalized self.onesided = onesided self.register_buffer("window", getattr(torch, window)(self.win_length)) self.eps = eps fmin = 0 if fmin is None else fmin fmax = fs / 2 if fmax is None else fmax melmat = librosa.filters.mel( sr=fs, n_fft=fft_size, n_mels=num_mels, fmin=fmin, fmax=fmax, ) self.register_buffer("melmat", torch.from_numpy(melmat.T).float()) self.log_base = log_base if self.log_base is None: self.log = torch.log elif self.log_base == 2.0: self.log = torch.log2 elif self.log_base == 10.0: self.log = torch.log10 else: raise ValueError(f"log_base: {log_base} is not supported.") def forward(self, x): """Calculate Mel-spectrogram. Args: x (Tensor): Input waveform tensor (B, T) or (B, C, T). Returns: Tensor: Mel-spectrogram (B, #mels, #frames). """ if x.dim() == 3: # (B, C, T) -> (B*C, T) x = x.reshape(-1, x.size(2)) x_stft = torch.stft(x, self.fft_size, self.hop_size, self.win_length, self.window, return_complex=True) x_power = x_stft.real ** 2 + x_stft.imag ** 2 x_amp = torch.sqrt(torch.clamp(x_power, min=self.eps)).transpose(2, 1) # (B, D, T') -> (B, T', D) x_mel = torch.matmul(x_amp, self.melmat) x_mel = torch.clamp(x_mel, min=self.eps) return self.log(x_mel).transpose(1, 2) # (B, D, T') class MultiMelSpectrogramLoss(torch.nn.Module): """Multi resolution Mel-spectrogram loss.""" def __init__( self, fs=22050, fft_sizes=[1024, 2048, 512], hop_sizes=[120, 240, 50], win_lengths=[600, 1200, 240], window="hann_window", num_mels=80, fmin=80, fmax=7600, center=True, normalized=False, onesided=True, eps=1e-10, log_base=10.0, ): """Initialize Mel-spectrogram loss.""" super().__init__() assert len(fft_sizes) == len(hop_sizes) == len(win_lengths) self.mel_transfers = torch.nn.ModuleList() for fft_size, hop_size, win_length in zip(fft_sizes, hop_sizes, win_lengths): self.mel_transfers += [ MelSpectrogram( fs=fs, fft_size=fft_size, hop_size=hop_size, win_length=win_length, window=window, num_mels=num_mels, fmin=fmin, fmax=fmax, center=center, normalized=normalized, onesided=onesided, eps=eps, log_base=log_base, ) ] def forward(self, y_hat, y): """Calculate Mel-spectrogram loss. Args: y_hat (Tensor): Generated single tensor (B, C, T). y (Tensor): Groundtruth single tensor (B, C, T). Returns: Tensor: Mel-spectrogram loss value. """ mel_loss = 0.0 for f in self.mel_transfers: mel_loss += F.l1_loss(f(y_hat), f(y)) mel_loss /= len(self.mel_transfers) return mel_loss
AudioDec-main
losses/mel_loss.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. """Waveform-based loss modules.""" import torch class WaveformShapeLoss(torch.nn.Module): """Waveform shape loss.""" def __init__(self, winlen): super().__init__() self.loss = torch.nn.L1Loss() self.winlen = winlen self.maxpool = torch.nn.MaxPool1d(self.winlen) def forward(self, y_hat, y): """Calculate L1 loss. Args: y_hat (Tensor): Generated single tensor (B, 1, T). y (Tensor): Groundtruth single tensor (B, 1, T). Returns: Tensor: L1 loss value. """ ys = self.maxpool(torch.abs(y)) ys_hat = self.maxpool(torch.abs(y_hat)) loss = self.loss(ys_hat, ys) return loss class MultiWindowShapeLoss(torch.nn.Module): """Multi-window-lengthe waveform shape loss.""" def __init__( self, winlen=[300, 200, 100], ): """Initialize Multi window shape loss module. Args: winlen (list): List of window lengths. """ super(MultiWindowShapeLoss, self).__init__() self.shape_losses = torch.nn.ModuleList() for wl in winlen: self.shape_losses += [WaveformShapeLoss(wl)] def forward(self, y_hat, y): """Calculate L1 loss. Args: y_hat (Tensor): Generated single tensor (B, 1, T). y (Tensor): Groundtruth single tensor (B, 1, T). Returns: Tensor: L2 loss value. """ loss = 0.0 for f in self.shape_losses: loss += f(y_hat, y) loss /= len(self.shape_losses) return loss
AudioDec-main
losses/waveform_loss.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright 2021 Tomoki Hayashi # MIT License (https://opensource.org/licenses/MIT) """Feature matching loss modules.""" import torch import torch.nn.functional as F class FeatureMatchLoss(torch.nn.Module): """Feature matching loss module.""" def __init__( self, average_by_layers=True, average_by_discriminators=True, include_final_outputs=False, ): """Initialize FeatureMatchLoss module.""" super().__init__() self.average_by_layers = average_by_layers self.average_by_discriminators = average_by_discriminators self.include_final_outputs = include_final_outputs def forward(self, feats_hat, feats): """Calcualate feature matching loss. Args: feats_hat (list): List of list of discriminator outputs calcuated from generater outputs. feats (list): List of list of discriminator outputs calcuated from groundtruth. Returns: Tensor: Feature matching loss value. """ feat_match_loss = 0.0 for i, (feats_hat_, feats_) in enumerate(zip(feats_hat, feats)): feat_match_loss_ = 0.0 if not self.include_final_outputs: feats_hat_ = feats_hat_[:-1] feats_ = feats_[:-1] for j, (feat_hat_, feat_) in enumerate(zip(feats_hat_, feats_)): feat_match_loss_ += F.l1_loss(feat_hat_, feat_.detach()) if self.average_by_layers: feat_match_loss_ /= j + 1 feat_match_loss += feat_match_loss_ if self.average_by_discriminators: feat_match_loss /= i + 1 return feat_match_loss
AudioDec-main
losses/feat_match_loss.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # # Reference (https://github.com/kan-bayashi/ParallelWaveGAN/) """Convolution layers.""" import torch import torch.nn as nn import torch.nn.functional as F class Conv1d1x1(nn.Conv1d): """1x1 Conv1d.""" def __init__(self, in_channels, out_channels, bias=True): super(Conv1d1x1, self).__init__(in_channels, out_channels, kernel_size=1, bias=bias) class NonCausalConv1d(nn.Module): """1D noncausal convloution w/ 2-sides padding.""" def __init__( self, in_channels, out_channels, kernel_size, stride=1, padding=-1, dilation=1, groups=1, bias=True): super().__init__() self.in_channels = in_channels self.out_channels = out_channels self.kernel_size = kernel_size if padding < 0: padding = (kernel_size - 1) // 2 * dilation self.dilation = dilation self.conv = nn.Conv1d( in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias, ) def forward(self, x): """ Args: x (Tensor): Float tensor variable with the shape (B, C, T). Returns: Tensor: Float tensor variable with the shape (B, C, T). """ x = self.conv(x) return x class NonCausalConvTranspose1d(nn.Module): """1D noncausal transpose convloution.""" def __init__( self, in_channels, out_channels, kernel_size, stride, padding=-1, output_padding=-1, groups=1, bias=True, ): super().__init__() if padding < 0: padding = (stride+1) // 2 if output_padding < 0: output_padding = 1 if stride % 2 else 0 self.deconv = nn.ConvTranspose1d( in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=padding, output_padding=output_padding, groups=groups, bias=bias, ) def forward(self, x): """ Args: x (Tensor): Float tensor variable with the shape (B, C, T). Returns: Tensor: Float tensor variable with the shape (B, C', T'). """ x = self.deconv(x) return x class CausalConv1d(NonCausalConv1d): """1D causal convloution w/ 1-side padding.""" def __init__( self, in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1, bias=True, pad_buffer=None, ): super(CausalConv1d, self).__init__( in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=0, dilation=dilation, groups=groups, bias=bias, ) self.stride = stride self.pad_length = (kernel_size - 1) * dilation if pad_buffer is None: pad_buffer = torch.zeros(1, in_channels, self.pad_length) self.register_buffer("pad_buffer", pad_buffer) def forward(self, x): pad = nn.ConstantPad1d((self.pad_length, 0), 0.0) x = pad(x) return self.conv(x) def inference(self, x): x = torch.cat((self.pad_buffer, x), -1) self.pad_buffer = x[:, :, -self.pad_length:] return self.conv(x) def reset_buffer(self): self.pad_buffer.zero_() class CausalConvTranspose1d(NonCausalConvTranspose1d): """1D causal transpose convloution.""" def __init__( self, in_channels, out_channels, kernel_size, stride, bias=True, pad_buffer=None, ): super(CausalConvTranspose1d, self).__init__( in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, padding=0, output_padding=0, bias=bias, ) self.stride = stride self.pad_length = 1 if pad_buffer is None: pad_buffer = torch.zeros(1, in_channels, self.pad_length) self.register_buffer("pad_buffer", pad_buffer) def forward(self, x): pad = nn.ReplicationPad1d((self.pad_length, 0)) x = pad(x) return self.deconv(x)[:, :, self.stride : -self.stride] def inference(self, x): x = torch.cat((self.pad_buffer, x), -1) self.pad_buffer = x[:, :, -self.pad_length:] return self.deconv(x)[:, :, self.stride : -self.stride] def reset_buffer(self): self.pad_buffer.zero_()
AudioDec-main
layers/conv_layer.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # # Reference (https://github.com/lucidrains/vector-quantize-pytorch/) """Vector quantizer.""" import torch import torch.nn as nn import torch.nn.functional as F class VectorQuantize(nn.Module): """Vector quantization w/ exponential moving averages (EMA)""" def __init__( self, dim, codebook_size, decay = 0.8, commitment = 1., eps = 1e-5, n_embed = None, ): super().__init__() n_embed = self.default(n_embed, codebook_size) self.dim = dim self.n_embed = n_embed self.decay = decay self.eps = eps self.commitment = commitment embed = torch.randn(dim, n_embed) self.register_buffer('embed', embed) self.register_buffer('cluster_size', torch.zeros(n_embed)) self.register_buffer('embed_avg', embed.clone()) @property def codebook(self): return self.embed.transpose(0, 1) def exists(self,val): return val is not None def default(self, val, d): return val if self.exists(val) else d def ema_inplace(self, moving_avg, new, decay): moving_avg.data.mul_(decay).add_(new, alpha = (1 - decay)) def laplace_smoothing(self, x, n_categories, eps=1e-5): return (x + eps) / (x.sum() + n_categories * eps) def forward(self, input): dtype = input.dtype flatten = input.reshape(-1, self.dim) dist = ( flatten.pow(2).sum(1, keepdim=True) - 2 * flatten @ self.embed + self.embed.pow(2).sum(0, keepdim=True) ) _, embed_ind = (-dist).max(1) embed_onehot = F.one_hot(embed_ind, self.n_embed).type(dtype) embed_ind = embed_ind.view(*input.shape[:-1]) quantize = F.embedding(embed_ind, self.embed.transpose(0, 1)) if self.training: self.ema_inplace(self.cluster_size, embed_onehot.sum(0), self.decay) embed_sum = flatten.transpose(0, 1) @ embed_onehot self.ema_inplace(self.embed_avg, embed_sum, self.decay) cluster_size = self.laplace_smoothing(self.cluster_size, self.n_embed, self.eps) * self.cluster_size.sum() embed_normalized = self.embed_avg / cluster_size.unsqueeze(0) self.embed.data.copy_(embed_normalized) loss = F.mse_loss(quantize.detach(), input) * self.commitment quantize = input + (quantize - input).detach() avg_probs = torch.mean(embed_onehot, dim=0) perplexity = torch.exp(-torch.sum(avg_probs * torch.log(avg_probs + 1e-10))) return quantize, loss, perplexity def forward_index(self, input): dtype = input.dtype flatten = input.reshape(-1, self.dim) dist = ( flatten.pow(2).sum(1, keepdim=True) - 2 * flatten @ self.embed + self.embed.pow(2).sum(0, keepdim=True) ) _, embed_ind = (-dist).max(1) embed_onehot = F.one_hot(embed_ind, self.n_embed).type(dtype) embed_ind = embed_ind.view(*input.shape[:-1]) quantize = F.embedding(embed_ind, self.embed.transpose(0, 1)) quantize = input + (quantize - input).detach() return quantize, embed_ind class ResidualVQ(nn.Module): """ Residual VQ following algorithm 1. in https://arxiv.org/pdf/2107.03312.pdf """ def __init__( self, *, num_quantizers, **kwargs ): super().__init__() self.layers = nn.ModuleList([VectorQuantize(**kwargs) for _ in range(num_quantizers)]) def forward(self, x): quantized_out = 0. residual = x all_losses = [] all_perplexities = [] for layer in self.layers: quantized, loss, perplexity = layer(residual) # Issue: https://github.com/lucidrains/vector-quantize-pytorch/issues/33 # We found considering only the 1st layer VQ's graident results in better performance #residual = residual - quantized.detach() # considering all layers' graidents residual = residual - quantized # considering only the first layer's graident quantized_out = quantized_out + quantized all_losses.append(loss) all_perplexities.append(perplexity) all_losses, all_perplexities = map(torch.stack, (all_losses, all_perplexities)) return quantized_out, all_losses, all_perplexities def forward_index(self, x, flatten_idx=False): quantized_out = 0. residual = x all_indices = [] for i, layer in enumerate(self.layers): quantized, indices = layer.forward_index(residual) #residual = residual - quantized.detach() residual = residual - quantized quantized_out = quantized_out + quantized if flatten_idx: indices += (self.codebook_size * i) all_indices.append(indices) all_indices= torch.stack(all_indices) return quantized_out, all_indices.squeeze(1) def initial(self): self.codebook = [] for layer in self.layers: self.codebook.append(layer.codebook) self.codebook_size = self.codebook[0].size(0) self.codebook = torch.stack(self.codebook) self.codebook = self.codebook.reshape(-1, self.codebook.size(-1)) def lookup(self, indices): quantized_out = F.embedding(indices, self.codebook) # Num x T x C return torch.sum(quantized_out, dim=0,keepdim=True)
AudioDec-main
layers/vq_module.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # # Reference (https://github.com/kan-bayashi/ParallelWaveGAN/) """Testing stage template.""" import os import abc import sys import time import yaml import torch import logging import soundfile as sf from tqdm import tqdm from bin.utils import load_config class TestGEN(abc.ABC): def __init__( self, args, ): # set logger logging.basicConfig( level=logging.INFO, stream=sys.stdout, format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s", ) # device if not torch.cuda.is_available(): self.device = torch.device('cpu') logging.info(f"device: cpu") else: self.device = torch.device('cuda') logging.info(f"device: gpu") # initialize attribute if hasattr(args, 'encoder'): self.encoder_checkpoint = args.encoder self.encoder_config = self._load_config(args.encoder) if hasattr(args, 'decoder'): self.decoder_checkpoint = args.decoder self.decoder_config = self._load_config(args.decoder) self.encoder = None self.decoder = None self.dataset = None self.outdir = None @abc.abstractmethod def initial_folder(self, output_name): pass @abc.abstractmethod def load_dataset(self): pass @abc.abstractmethod def load_encoder(self): pass @abc.abstractmethod def load_decoder(self): pass @abc.abstractmethod def encode(self, x): pass @abc.abstractmethod def decode(self, z): pass def run(self): total_rtf = 0.0 with torch.no_grad(), tqdm(self.dataset, desc="[test]") as pbar: for idx, (utt_id, x) in enumerate(pbar, 1): start = time.time() zq = self.encode(x) y = self.decode(zq) y = y.squeeze(1).transpose(1, 0).cpu().numpy() # T x C rtf = (time.time() - start) / (len(y) / self.decoder_config['sampling_rate']) pbar.set_postfix({"RTF": rtf}) total_rtf += rtf # output wav file self._save_wav(os.path.join(self.outdir, f"{utt_id}_output.wav"), y) logging.info( "Finished generation of %d utterances (RTF = %.03f)." % (idx, (total_rtf / idx)) ) def _save_wav(self, file_name, audio): sf.write( file_name, audio, self.decoder_config['sampling_rate'], "PCM_16", ) def _load_config(self, checkpoint, config_name='config.yml'): return load_config(checkpoint, config_name)
AudioDec-main
bin/test.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import os import abc import yaml import time import queue import threading import torch import torchaudio import numpy as np from typing import Union class AudioCodec(abc.ABC): def __init__( self, tx_device: str = "cpu", rx_device: str = "cpu", receptive_length: int = 8192, ): self.tx_device = tx_device self.rx_device = rx_device self.receptive_length = receptive_length self.tx_encoder = None self.rx_encoder = None self.decoder = None @abc.abstractmethod def _load_encoder(self, checkpoint): pass @abc.abstractmethod def _load_decoder(self, checkpoint): pass def _load_config(self, checkpoint, config_name='config.yml'): dirname = os.path.dirname(checkpoint) config_path = os.path.join(dirname, config_name) with open(config_path) as f: config = yaml.load(f, Loader=yaml.Loader) return config def load_transmitter(self, encoder_checkpoint): # load transmitter model(s) assert os.path.exists(encoder_checkpoint), f'{encoder_checkpoint} does not exist!' self.tx_encoder = self._load_encoder(encoder_checkpoint) self.tx_encoder.eval().to(self.tx_device) self.tx_encoder.initial_encoder(self.receptive_length, self.tx_device) print("Load tx_encoder: %s" % (encoder_checkpoint)) def load_receiver(self, encoder_checkpoint, decoder_checkpoint): # load receiver model(s) assert os.path.exists(encoder_checkpoint), f'{encoder_checkpoint} does not exist!' self.rx_encoder = self._load_encoder(encoder_checkpoint) self.rx_encoder.eval().to(self.rx_device) zq = self.rx_encoder.initial_encoder(self.receptive_length, self.rx_device) print("Load rx_encoder: %s" % (encoder_checkpoint)) assert os.path.exists(decoder_checkpoint), f'{decoder_checkpoint} does not exist!' self.decoder = self._load_decoder(decoder_checkpoint) self.decoder.eval().to(self.rx_device) self.decoder.initial_decoder(zq) print("Load decoder: %s" % (decoder_checkpoint)) class AudioCodecStreamer(abc.ABC): """ Streams audio from an input microphone to headpones/speakers. For each model that can be optionally provided (encoder, decoder), the input audio is processed by the forward call of these models. Main functions (see function definition for detailed documentation): * __init__ * enable_filedump * set_tx_rx_poses * run Example usage: streamer = AudioCodecStreamer( input_device=1, output_device=4, frame_size=512, encoder=my_encoder_network, tx_device="cuda:0", decoder=my_decoder_network, rx_device="cuda:1", ) streamer.enable_filedump(input_stream_file="input.wav", output_stream_file="output.wav") streamer.run() """ def __init__( self, input_device: Union[str, int], output_device: Union[str, int], input_channels: int = 1, output_channels: int = 1, frame_size: int = 512, sample_rate: int = 48000, gain: int = 1.0, max_latency: float = 0.1, # Transmitter params tx_encoder = None, tx_device: str = "cpu", # Receiver params rx_encoder = None, decoder = None, rx_device: str = "cpu", ): """ Sounddevice parameters :param input_device: int or str, name or index of the input device. To get a list of all input devices call python3 -m sounddevice. :param output_device: int or str, name or index of the output device. To get a list of all output devices call python3 -m sounddevice. :param input_channels: number of input channels, usually 1 but might be multiple microphones as well :param output_channels: number of output channels, usually 2 for binaural audio :param frame_size: number of audio samples in a frame :param sample_rate: sample rate of the audio signal :param gain: linear factor to scale the input audio by :param max_latency: maximal accepted latency in seconds before frames get dropped ####### Transmitter parameters :param tx_encoder: encoder network in the transimtter side Is an instance of torch.nn.Module and must be fully initialized and loaded. Must have a forward function that expects a batch x input_channels x frame_size tensor as input. Default: None (input tensor is forwarded to decoder without change) :param tx_device: device on transmitter (cpu, cuda:0, cuda:1, ...) ####### Receiver parameters :param rx_encoder: encoder network in the receiver side Is an instance of torch.nn.Module and must be fully initialized and loaded. Must have a forward function that expects a batch x input_channels x frame_size tensor as input. Default: None (input tensor is forwarded to decoder without change) :param decoder: decoder network Is an instance of torch.nn.Module and must be fully initialized and loaded. Must have a forward function that expects a tensor of the shape produced by the encoder. Default: None (input tensor is forwarded to binauralizer without change) :param rx_device: device on receiver (cpu, cuda:0, cuda:1, ...) """ self.input_device = input_device self.output_device = output_device self.input_channels = input_channels self.output_channels = output_channels self.frame_size = frame_size self.sample_rate = sample_rate self.gain = gain self.max_latency = max_latency # encoder self.tx_encoder = tx_encoder self.tx_device = tx_device print(f'Encoder device: {tx_device}') # decoder self.rx_encoder = rx_encoder self.decoder = decoder self.rx_device = rx_device print(f'Decoder device: {rx_device}') # queues for encoder, decoder, and output self.encoder_queue = queue.Queue() self.decoder_queue = queue.Queue() self.output_queue = queue.Queue() # file dump if requested self.input_dump = [] self.output_dump = [] self.input_dump_filename = None self.output_dump_filename = None # streaming statistics self.frame_drops = 0 self.n_frames = 0 self.encoder_times = [] self.decoder_times = [] self.latency_queue = queue.Queue() self.latencies = [] @abc.abstractmethod def _encode(self, x): pass @abc.abstractmethod def _decode(self, x): pass def _run_encoder(self): while threading.main_thread().is_alive(): try: x = self.encoder_queue.get(timeout=1) except: continue start = time.time() x = x.to(self.tx_device) with torch.no_grad(): if self.tx_encoder is not None: x = self._encode(x) self.encoder_times.append(time.time() - start) self.decoder_queue.put(x) def _run_decoder(self): while threading.main_thread().is_alive(): try: x = self.decoder_queue.get(timeout=1) except: continue start = time.time() x = x.to(self.rx_device) with torch.no_grad(): if (self.rx_encoder is not None) and (self.decoder is not None): x = self._decode(x) self.decoder_times.append(time.time() - start) self.output_queue.put(x) def _process(self, data): data = data * self.gain input_data = torch.from_numpy(data).transpose(1, 0).contiguous() # channels x frame_size if self.input_dump_filename is not None: self.input_dump.append(input_data) # add batch dimension input_data = input_data.unsqueeze(0) # process data self.encoder_queue.put(input_data) self.latency_queue.put(time.time()) try: output_data = self.output_queue.get_nowait() latency = time.time() - self.latency_queue.get_nowait() self.latencies.append(latency) # clear queues if latency get too high; this will lead to frame drops if latency > self.max_latency: self.encoder_queue.queue.clear() self.decoder_queue.queue.clear() self.output_queue.queue.clear() while not self.latency_queue.empty(): self.frame_drops += 1 self.latency_queue.get_nowait() except queue.Empty: output_data = torch.zeros(1, self.output_channels, self.frame_size) output_data = output_data.squeeze(0).detach().cpu() self.n_frames += 1 if self.output_dump_filename is not None: self.output_dump.append(output_data) data = output_data.transpose(1, 0).contiguous().numpy() return data def _callback(self, indata, outdata, frames, _time, status): if status: print(status) outdata[:] = self._process(indata) def _exit(self): # dump data to file if required if self.input_dump_filename is not None: audio = torch.clamp(torch.cat(self.input_dump, dim=-1), min=-1, max=1) torchaudio.save(self.input_dump_filename, audio, self.sample_rate) if self.output_dump_filename is not None: audio = torch.clamp(torch.cat(self.output_dump, dim=-1), min=-1, max=1) torchaudio.save(self.output_dump_filename, audio, self.sample_rate) # compute statistics with threading.Lock(): encoder_mean = np.mean(np.array(self.encoder_times) * 1000.0) encoder_std = np.std(np.array(self.encoder_times) * 1000.0) decoder_mean = np.mean(np.array(self.decoder_times) * 1000.0) decoder_std = np.std(np.array(self.decoder_times) * 1000.0) latency_mean = np.mean(np.array(self.latencies) * 1000.0) latency_std = np.std(np.array(self.latencies) * 1000.0) frame_drops_ratio = self.frame_drops / self.n_frames # print statistics print('#' * 80) print(f"encoder processing time (ms): {encoder_mean:.2f} +- {encoder_std:.2f}") print(f"decoder processing time (ms): {decoder_mean:.2f} +- {decoder_std:.2f}") print(f"system latency (ms): {latency_mean:.2f} +- {latency_std:.2f}") print(f"frame drops: {self.frame_drops} ({frame_drops_ratio * 100:.2f}%)") print('#' * 80) def enable_filedump(self, input_stream_file: str = None, output_stream_file: str = None): """ dumps input/output audio to file if input/output filenames are specified call this function before run() :param input_stream_file: name of the file to dump input audio to :param output_stream_file: name of the file to dump output audio to at least one of the files needs to be specified """ if input_stream_file is None and output_stream_file is None: raise Exception("At least one of input_stream_file and output_stream_file must be specified.") if input_stream_file is not None: if not input_stream_file[-4:] == ".wav": input_stream_file += ".wav" self.input_dump_filename = input_stream_file if output_stream_file is not None: if not output_stream_file[-4:] == ".wav": output_stream_file += ".wav" self.output_dump_filename = output_stream_file def run(self, latency): """ start streaming from the input device and forward the processed audio to the output device prints statistics about mean processing time, standard deviation of each processing pass, and percentage of buffer underflows """ # start encoder and decoder threads encoder_thread = threading.Thread(target=self._run_encoder, daemon=True) encoder_thread.start() decoder_thread = threading.Thread(target=self._run_decoder, daemon=True) decoder_thread.start() try: # import device import sounddevice as sd with sd.Stream( device=(self.input_device, self.output_device), samplerate=self.sample_rate, blocksize=self.frame_size, dtype=np.float32, latency=latency, channels=(self.input_channels, self.output_channels), callback=self._callback ): print('### starting stream [press Return to quit] ###') input() self._exit() except KeyboardInterrupt: self._exit() except Exception as e: print(type(e).__name__ + ': ' + str(e))
AudioDec-main
bin/stream.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. """Utility modules.""" import os import yaml def load_config(checkpoint, config_name='config.yml'): dirname = os.path.dirname(checkpoint) config_path = os.path.join(dirname, config_name) with open(config_path) as f: config = yaml.load(f, Loader=yaml.Loader) return config
AudioDec-main
bin/utils.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # # Reference (https://github.com/kan-bayashi/ParallelWaveGAN/) """Training stage template.""" import os import abc import sys import yaml import random import logging import torch import numpy as np from bin.utils import load_config class TrainGAN(abc.ABC): def __init__( self, args, ): # set logger logging.basicConfig( level=logging.INFO, stream=sys.stdout, format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s", ) # Fix seed and make backends deterministic random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) if not torch.cuda.is_available(): self.device = torch.device('cpu') logging.info(f"device: cpu") else: self.device = torch.device('cuda') logging.info(f"device: gpu") torch.cuda.manual_seed_all(args.seed) if args.disable_cudnn == "False": torch.backends.cudnn.benchmark = True # initialize config with open(args.config, 'r') as f: self.config = yaml.load(f, Loader=yaml.FullLoader) self.config.update(vars(args)) # initialize model folder expdir = os.path.join(args.exp_root, args.tag) os.makedirs(expdir, exist_ok=True) self.config["outdir"] = expdir # save config with open(os.path.join(expdir, "config.yml"), "w") as f: yaml.dump(self.config, f, Dumper=yaml.Dumper) for key, value in self.config.items(): logging.info(f"[TrainGAN] {key} = {value}") # initialize attribute self.resume = args.resume self.data_loader = None self.model = {} self.criterion = None self.optimizer = None self.scheduler = None self.trainer = None # initialize batch_length self.batch_length = self.config['batch_length'] @abc.abstractmethod def initialize_data_loader(self): pass @abc.abstractmethod def define_model(self): pass @abc.abstractmethod def define_trainer(self): pass @abc.abstractmethod def initialize_model(self): pass @abc.abstractmethod def define_criterion(self): pass def run(self): try: logging.info(f"The current training step: {self.trainer.steps}") self.trainer.train_max_steps = self.config["train_max_steps"] if not self.trainer._check_train_finish(): self.trainer.run() if self.config.get("adv_train_max_steps", False) and self.config.get("adv_batch_length", False): self.batch_length = self.config['adv_batch_length'] logging.info(f"Reload dataloader for adversarial training.") self.initialize_data_loader() self.trainer.data_loader = self.data_loader self.trainer.train_max_steps = self.config["adv_train_max_steps"] self.trainer.run() finally: self.trainer.save_checkpoint( os.path.join(self.config["outdir"], f"checkpoint-{self.trainer.steps}steps.pkl") ) logging.info(f"Successfully saved checkpoint @ {self.trainer.steps}steps.") def _show_setting(self): logging.info(self.model['generator']) logging.info(self.model['discriminator']) logging.info(self.optimizer['generator']) logging.info(self.optimizer['discriminator']) logging.info(self.scheduler['generator']) logging.info(self.scheduler['discriminator']) for criterion_ in self.criterion.values(): logging.info(criterion_) def _load_config(self, checkpoint, config_name='config.yml'): return load_config(checkpoint, config_name)
AudioDec-main
bin/train.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import os import torch from typing import Union from models.autoencoder.AudioDec import StreamGenerator as generator_audiodec from models.vocoder.HiFiGAN import StreamGenerator as generator_hifigan from bin.stream import AudioCodec, AudioCodecStreamer class AudioDec(AudioCodec): def __init__( self, tx_device: str = "cpu", rx_device: str = "cpu", receptive_length: int = 8192, # actual number is 7209 for symAD_vctk_48000_hop300 ): super(AudioDec, self).__init__( tx_device = tx_device, rx_device = rx_device, receptive_length = receptive_length, ) def _load_encoder(self, checkpoint): # load config config = self._load_config(checkpoint) # load model if config['model_type'] in ['symAudioDec', 'symAudioDecUniv']: encoder = generator_audiodec else: raise NotImplementedError(f"Encoder type {config['model_type']} is not supported!") encoder = encoder(**config['generator_params']) encoder.load_state_dict(torch.load(checkpoint, map_location='cpu')['model']['generator']) return encoder def _load_decoder(self, checkpoint): # load config config = self._load_config(checkpoint) # load model if config['model_type'] in ['symAudioDec', 'symAudioDecUniv']: decoder = generator_audiodec elif config['model_type'] in ['HiFiGAN', 'UnivNet']: decoder = generator_hifigan else: raise NotImplementedError(f"Decoder {config['model_type']} is not supported!") decoder = decoder(**config['generator_params']) decoder.load_state_dict(torch.load(checkpoint, map_location='cpu')['model']['generator']) return decoder class AudioDecStreamer(AudioCodecStreamer): def __init__( self, input_device: Union[str, int], output_device: Union[str, int], input_channels: int = 1, output_channels: int = 1, frame_size: int = 512, sample_rate: int = 48000, gain: int = 1.0, max_latency: float = 0.1, # encoder params tx_encoder = None, tx_device: str = "cpu", # decoder params rx_encoder = None, decoder = None, rx_device: str = "cpu", ): super(AudioDecStreamer, self).__init__( input_device = input_device, output_device = output_device, input_channels = input_channels, output_channels = output_channels, frame_size = frame_size, sample_rate = sample_rate, gain = gain, max_latency = max_latency, tx_encoder = tx_encoder, tx_device = tx_device, rx_encoder = rx_encoder, decoder = decoder, rx_device = rx_device, ) def _encode(self, x): x = self.tx_encoder.encode(x) return self.tx_encoder.quantize(x) def _decode(self, x): x = self.rx_encoder.lookup(x) return self.decoder.decode(x) def assign_model(model): if model == 'libritts_v1': sample_rate = 24000 tx_steps = 500000 rx_steps = 500000 encoder_checkpoint = os.path.join('exp', 'autoencoder', 'symAD_libritts_24000_hop300', f"checkpoint-{tx_steps}steps.pkl") decoder_checkpoint = os.path.join('exp', 'vocoder', 'AudioDec_v1_symAD_libritts_24000_hop300_clean', f"checkpoint-{rx_steps}steps.pkl") elif model == 'libritts_sym': sample_rate = 24000 tx_steps = 500000 rx_steps = 1000000 encoder_checkpoint = os.path.join('exp', 'autoencoder', 'symAD_libritts_24000_hop300', f"checkpoint-{tx_steps}steps.pkl") decoder_checkpoint = os.path.join('exp', 'autoencoder', 'symAD_libritts_24000_hop300', f"checkpoint-{rx_steps}steps.pkl") elif model == 'vctk_v1': sample_rate = 48000 tx_steps = 200000 rx_steps = 500000 encoder_checkpoint = os.path.join('exp', 'autoencoder', 'symAD_vctk_48000_hop300', f"checkpoint-{tx_steps}steps.pkl") decoder_checkpoint = os.path.join('exp', 'vocoder', 'AudioDec_v1_symAD_vctk_48000_hop300_clean', f"checkpoint-{rx_steps}steps.pkl") elif model == 'vctk_sym': sample_rate = 48000 tx_steps = 200000 rx_steps = 700000 encoder_checkpoint = os.path.join('exp', 'autoencoder', 'symAD_vctk_48000_hop300', f"checkpoint-{tx_steps}steps.pkl") decoder_checkpoint = os.path.join('exp', 'autoencoder', 'symAD_vctk_48000_hop300', f"checkpoint-{rx_steps}steps.pkl") elif model == 'vctk_v0': sample_rate = 48000 tx_steps = 200000 rx_steps = 500000 encoder_checkpoint = os.path.join('exp', 'autoencoder', 'symAD_vctk_48000_hop300', f"checkpoint-{tx_steps}steps.pkl") decoder_checkpoint = os.path.join('exp', 'vocoder', 'AudioDec_v0_symAD_vctk_48000_hop300_clean', f"checkpoint-{rx_steps}steps.pkl") elif model == 'vctk_v2': sample_rate = 48000 tx_steps = 200000 rx_steps = 500000 encoder_checkpoint = os.path.join('exp', 'autoencoder', 'symAD_vctk_48000_hop300', f"checkpoint-{tx_steps}steps.pkl") decoder_checkpoint = os.path.join('exp', 'vocoder', 'AudioDec_v2_symAD_vctk_48000_hop300_clean', f"checkpoint-{rx_steps}steps.pkl") elif model == 'vctk_denoise': sample_rate = 48000 tx_steps = 200000 rx_steps = 500000 encoder_checkpoint = os.path.join('exp', 'denoise', 'symAD_vctk_48000_hop300', f"checkpoint-{tx_steps}steps.pkl") decoder_checkpoint = os.path.join('exp', 'vocoder', 'AudioDec_v1_symAD_vctk_48000_hop300_clean', f"checkpoint-{rx_steps}steps.pkl") elif model == 'vctk_univ': sample_rate = 48000 tx_steps = 500000 rx_steps = 500000 encoder_checkpoint = os.path.join('exp', 'autoencoder', 'symADuniv_vctk_48000_hop300', f"checkpoint-{tx_steps}steps.pkl") decoder_checkpoint = os.path.join('exp', 'vocoder', 'AudioDec_v3_symADuniv_vctk_48000_hop300_clean', f"checkpoint-{rx_steps}steps.pkl") elif model == 'vctk_univ_sym': sample_rate = 48000 tx_steps = 500000 rx_steps = 1000000 encoder_checkpoint = os.path.join('exp', 'autoencoder', 'symADuniv_vctk_48000_hop300', f"checkpoint-{tx_steps}steps.pkl") decoder_checkpoint = os.path.join('exp', 'autoencoder', 'symADuniv_vctk_48000_hop300', f"checkpoint-{rx_steps}steps.pkl") else: raise NotImplementedError(f'Model {model} is not supported!') return sample_rate, encoder_checkpoint, decoder_checkpoint
AudioDec-main
utils/audiodec.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. """Utility modules.""" def check_mode(mode, method): stream_modes = ['causal'] assert mode in stream_modes, f"Mode {mode} does not support {method}!"
AudioDec-main
models/utils.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # # Reference (https://github.com/kan-bayashi/ParallelWaveGAN/) """AudioDec model.""" import torch import inspect from layers.conv_layer import CausalConv1d, CausalConvTranspose1d from models.autoencoder.modules.encoder import Encoder from models.autoencoder.modules.decoder import Decoder from models.autoencoder.modules.projector import Projector from models.autoencoder.modules.quantizer import Quantizer from models.utils import check_mode ### GENERATOR ### class Generator(torch.nn.Module): """AudioDec generator.""" def __init__( self, input_channels=1, output_channels=1, encode_channels=32, decode_channels=32, code_dim=64, codebook_num=8, codebook_size=1024, bias=True, enc_ratios=(2, 4, 8, 16), dec_ratios=(16, 8, 4, 2), enc_strides=(3, 4, 5, 5), dec_strides=(5, 5, 4, 3), mode='causal', codec='audiodec', projector='conv1d', quantier='residual_vq', ): super().__init__() if codec == 'audiodec': encoder = Encoder decoder = Decoder else: raise NotImplementedError(f"Codec ({codec}) is not supported!") self.mode = mode self.input_channels = input_channels self.encoder = encoder( input_channels=input_channels, encode_channels=encode_channels, channel_ratios=enc_ratios, strides=enc_strides, kernel_size=7, bias=bias, mode=self.mode, ) self.decoder = decoder( code_dim=code_dim, output_channels=output_channels, decode_channels=decode_channels, channel_ratios=dec_ratios, strides=dec_strides, kernel_size=7, bias=bias, mode=self.mode, ) self.projector = Projector( input_channels=self.encoder.out_channels, code_dim=code_dim, kernel_size=3, stride=1, bias=False, mode=self.mode, model=projector, ) self.quantizer = Quantizer( code_dim=code_dim, codebook_num=codebook_num, codebook_size=codebook_size, model=quantier, ) def forward(self, x): (batch, channel, length) = x.size() if channel != self.input_channels: x = x.reshape(-1, self.input_channels, length) # (B, C, T) -> (B', C', T) x = self.encoder(x) z = self.projector(x) zq, vqloss, perplexity = self.quantizer(z) y = self.decoder(zq) return y, zq, z, vqloss, perplexity # STREAMING class StreamGenerator(Generator): """AudioDec streaming generator.""" def __init__( self, input_channels=1, output_channels=1, encode_channels=32, decode_channels=32, code_dim=64, codebook_num=8, codebook_size=1024, bias=True, enc_ratios=(2, 4, 8, 16), dec_ratios=(16, 8, 4, 2), enc_strides=(3, 4, 5, 5), dec_strides=(5, 5, 4, 3), mode='causal', codec='audiodec', projector='conv1d', quantier='residual_vq', ): super(StreamGenerator, self).__init__( input_channels=input_channels, output_channels=output_channels, encode_channels=encode_channels, decode_channels=decode_channels, code_dim=code_dim, codebook_num=codebook_num, codebook_size=codebook_size, bias=bias, enc_ratios=enc_ratios, dec_ratios=dec_ratios, enc_strides=enc_strides, dec_strides=dec_strides, mode=mode, codec=codec, projector=projector, quantier=quantier, ) check_mode(mode, "AudioDec Streamer") self.reset_buffer() def initial_encoder(self, receptive_length, device): self.quantizer.initial() z = self.encode(torch.zeros(1, self.input_channels, receptive_length).to(device)) idx = self.quantize(z) zq = self.lookup(idx) return zq def initial_decoder(self, zq): self.decode(zq) def encode(self, x): (batch, channel, length) = x.size() if channel != self.input_channels: x = x.reshape(-1, self.input_channels, length) # (B, C, T) -> (B', C', T) x = self.encoder.encode(x) z = self.projector.encode(x) return z def quantize(self, z): zq, idx = self.quantizer.encode(z) return idx def lookup(self, idx): return self.quantizer.decode(idx) def decode(self, zq): return self.decoder.decode(zq.transpose(2, 1)) def reset_buffer(self): """Apply weight normalization module from all layers.""" def _reset_buffer(m): if isinstance(m, CausalConv1d) or isinstance(m, CausalConvTranspose1d): m.reset_buffer() self.apply(_reset_buffer)
AudioDec-main
models/autoencoder/AudioDec.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. # # Reference (https://ieeexplore.ieee.org/document/9625818) """Decoder modules.""" import torch import inspect from layers.conv_layer import NonCausalConv1d, NonCausalConvTranspose1d from layers.conv_layer import CausalConv1d, CausalConvTranspose1d from models.autoencoder.modules.residual_unit import NonCausalResidualUnit from models.autoencoder.modules.residual_unit import CausalResidualUnit from models.utils import check_mode class DecoderBlock(torch.nn.Module): """ Decoder block (upsampling) """ def __init__( self, in_channels, out_channels, stride, dilations=(1, 3, 9), bias=True, mode='causal', ): super().__init__() self.mode = mode if self.mode == 'noncausal': ResidualUnit = NonCausalResidualUnit ConvTranspose1d = NonCausalConvTranspose1d elif self.mode == 'causal': ResidualUnit = CausalResidualUnit ConvTranspose1d = CausalConvTranspose1d else: raise NotImplementedError(f"Mode ({self.mode}) is not supported!") self.conv = ConvTranspose1d( in_channels=in_channels, out_channels=out_channels, kernel_size=(2 * stride), stride=stride, bias=bias, ) self.res_units = torch.nn.ModuleList() for idx, dilation in enumerate(dilations): self.res_units += [ ResidualUnit(out_channels, out_channels, dilation=dilation)] self.num_res = len(self.res_units) def forward(self, x): x = self.conv(x) for idx in range(self.num_res): x = self.res_units[idx](x) return x def inference(self, x): check_mode(self.mode, inspect.stack()[0][3]) x = self.conv.inference(x) for idx in range(self.num_res): x = self.res_units[idx].inference(x) return x class Decoder(torch.nn.Module): def __init__(self, code_dim, output_channels, decode_channels, channel_ratios=(16, 8, 4, 2), strides=(5, 5, 4, 3), kernel_size=7, bias=True, mode='causal', ): super().__init__() assert len(channel_ratios) == len(strides) self.mode = mode if self.mode == 'noncausal': Conv1d = NonCausalConv1d elif self.mode == 'causal': Conv1d = CausalConv1d else: raise NotImplementedError(f"Mode ({self.mode}) is not supported!") self.conv1 = Conv1d( in_channels=code_dim, out_channels=(decode_channels * channel_ratios[0]), kernel_size=kernel_size, stride=1, bias=False) self.conv_blocks = torch.nn.ModuleList() for idx, stride in enumerate(strides): in_channels = decode_channels * channel_ratios[idx] if idx < (len(channel_ratios)-1): out_channels = decode_channels * channel_ratios[idx+1] else: out_channels = decode_channels self.conv_blocks += [ DecoderBlock(in_channels, out_channels, stride, bias=bias, mode=self.mode)] self.num_blocks = len(self.conv_blocks) self.conv2 = Conv1d(out_channels, output_channels, kernel_size, 1, bias=False) def forward(self, z): x = self.conv1(z) for i in range(self.num_blocks): x = self.conv_blocks[i](x) x = self.conv2(x) return x def decode(self, z): check_mode(self.mode, inspect.stack()[0][3]) x = self.conv1.inference(z) for i in range(self.num_blocks): x = self.conv_blocks[i].inference(x) x = self.conv2.inference(x) return x
AudioDec-main
models/autoencoder/modules/decoder.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (c) Meta Platforms, Inc. and affiliates. # All rights reserved. # # This source code is licensed under the license found in the # LICENSE file in the root directory of this source tree. import torch from layers.vq_module import ResidualVQ class Quantizer(torch.nn.Module): def __init__(self, code_dim, codebook_num, codebook_size, model='residual_vq', ): super().__init__() # speech if model == 'residual_vq': self.codebook = ResidualVQ(dim=code_dim, num_quantizers=codebook_num, codebook_size=codebook_size) else: raise NotImplementedError(f"Model ({model}) is not supported!") def initial(self): self.codebook.initial() def forward(self, z): zq, vqloss, perplexity = self.codebook(z.transpose(2, 1)) zq = zq.transpose(2, 1) return zq, vqloss, perplexity def inference(self, z): zq, indices = self.codebook.forward_index(z.transpose(2, 1)) zq = zq.transpose(2, 1) return zq, indices def encode(self, z): zq, indices = self.codebook.forward_index(z.transpose(2, 1), flatten_idx=True) return zq, indices def decode(self, indices): z = self.codebook.lookup(indices) return z
AudioDec-main
models/autoencoder/modules/quantizer.py